Exemplo n.º 1
0
    def executable_path(self, shell, env, view_settings, window_settings,
                        sublime_settings, executable_temp_files,
                        non_executable_temp_files, expected_debug,
                        expected_result):

        with GolangConfigMock(shell, env, view_settings, window_settings,
                              sublime_settings) as mock_context:

            mock_context.replace_tempdir_env()
            mock_context.replace_tempdir_view_settings()
            mock_context.replace_tempdir_window_settings()

            mock_context.make_executable_files(executable_temp_files)
            mock_context.make_files(non_executable_temp_files)

            if expected_result[0]:
                tempdir = mock_context.tempdir + os.sep
                expected_result = (expected_result[0].replace(
                    '{tempdir}', tempdir), expected_result[1])

            self.assertEquals(
                expected_result,
                golangconfig.executable_path('go', mock_context.view,
                                             mock_context.window))
            if expected_debug is None:
                self.assertEqual('', sys.stdout.getvalue())
            else:
                self.assertTrue(expected_debug in sys.stdout.getvalue())
Exemplo n.º 2
0
    def run(self, edit):
        view = self.view
        go_fmt_path, env = golangconfig.executable_path('gofmt', view=view)
        if go_fmt_path is None:
            go_fmt_path = 'gofmt'
        src = view.substr(sublime.Region(0, view.size()))
        gofmt = subprocess.Popen([go_fmt_path],
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        sout, serr = gofmt.communicate(src.encode())
        if gofmt.returncode != 0:
            print(serr.decode(), end="")
            return

        newsrc = sout.decode()
        diff = difflib.ndiff(src.splitlines(), newsrc.splitlines())
        i = 0
        for line in diff:
            if line.startswith("?"):  # skip hint lines
                continue

            l = (len(line) - 2) + 1
            if line.startswith("-"):
                diff_sanity_check(view.substr(sublime.Region(i, i + l - 1)),
                                  line[2:])
                view.erase(edit, sublime.Region(i, i + l))
            elif line.startswith("+"):
                view.insert(edit, i, line[2:] + "\n")
                i += l
            else:
                diff_sanity_check(view.substr(sublime.Region(i, i + l - 1)),
                                  line[2:])
                i += l
Exemplo n.º 3
0
 def test_executable_path_path_not_string(self):
     shell = '/bin/bash'
     env = {
         'PATH': '/bin'
     }
     view_settings = {
         'PATH': 1
     }
     with GolangConfigMock(shell, env, view_settings, None, {'debug': True}) as mock_context:
         self.assertEquals((None, None), golangconfig.executable_path('go', mock_context.view, mock_context.window))
         self.assertTrue('is not a string' in sys.stdout.getvalue())
Exemplo n.º 4
0
def plugin_loaded():
  # check if the dependent gotools have been installed
  tools = ["guru", "golint", "gocode", "gorename", "goimports"]

  missed_tools = ""
  for t in tools:
    if golangconfig.executable_path(t)[0] == None:
      missed_tools += '"%s" ' % t

  if missed_tools != "":
    print("\nGoTools Warning: %scan't be found in executable path.\nPlease \"go get\" them (Refer to README.md).\n" % missed_tools)
Exemplo n.º 5
0
def plugin_loaded():
  # check if the dependent gotools have been installed
  tools = ["guru", "golint", "gocode", "gorename", "goimports"]

  missed_tools = ""
  for t in tools:
    if golangconfig.executable_path(t)[0] == None:
      missed_tools += '"%s" ' % t

  if missed_tools != "":
    print("\nGoTools Warning: %scan't be found in executable path.\nPlease \"go get\" them (Refer to README.md).\n" % missed_tools)
Exemplo n.º 6
0
 def test_executable_path_path_not_string(self):
     shell = '/bin/bash'
     env = {'PATH': '/bin'}
     view_settings = {'PATH': 1}
     with GolangConfigMock(shell, env, view_settings, None,
                           {'debug': True}) as mock_context:
         self.assertEquals(
             (None, None),
             golangconfig.executable_path('go', mock_context.view,
                                          mock_context.window))
         self.assertTrue('is not a string' in sys.stdout.getvalue())
Exemplo n.º 7
0
    def executable_path(self, shell, env, view_settings, window_settings, sublime_settings,
                        executable_temp_files, non_executable_temp_files, expected_debug, expected_result):

        with GolangConfigMock(shell, env, view_settings, window_settings, sublime_settings) as mock_context:

            mock_context.replace_tempdir_env()
            mock_context.replace_tempdir_view_settings()
            mock_context.replace_tempdir_window_settings()

            mock_context.make_executable_files(executable_temp_files)
            mock_context.make_files(non_executable_temp_files)

            if expected_result[0]:
                tempdir = mock_context.tempdir + os.sep
                expected_result = (expected_result[0].replace('{tempdir}', tempdir), expected_result[1])

            self.assertEquals(
                expected_result,
                golangconfig.executable_path('go', mock_context.view, mock_context.window)
            )
            if expected_debug is None:
                self.assertEqual('', sys.stdout.getvalue())
            else:
                self.assertTrue(expected_debug in sys.stdout.getvalue())
Exemplo n.º 8
0
    def on_query_completions(self, view, prefix, locations):
        loc = locations[0]
        if not view.match_selector(loc, "source.go"):
            return None

        src = view.substr(sublime.Region(0, view.size()))
        filename = view.file_name()
        cloc = "c{0}".format(loc)
        go_code_path, env = golangconfig.executable_path('gocode', view=view)
        if go_code_path is None:
            go_code_path = 'gocode'
        gocode = subprocess.Popen(
            [go_code_path, "-f=csv", "autocomplete", filename, cloc],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE)
        out = gocode.communicate(src.encode())[0].decode()

        result = []
        for line in filter(bool, out.split("\n")):
            arg = line.split(",,")
            hint, subj = hint_and_subj(arg[0], arg[1], arg[2])
            result.append([hint, subj])

        return (result, sublime.INHIBIT_WORD_COMPLETIONS)