From c4006c691dd16dc97d9c3b8fe5318ae8f45449bd Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Tue, 22 Nov 2016 09:48:43 +0100 Subject: [PATCH 01/12] Make sure Req_Error is passed a string --- tern.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tern.py b/tern.py index 7c2de71..8f77c4f 100644 --- a/tern.py +++ b/tern.py @@ -264,7 +264,7 @@ def f(port, doc): req = opener.open("http://" + localhost + ":" + str(port) + "/", json.dumps(doc).encode("utf-8"), 1) return json.loads(req.read().decode("utf-8")) except urllib.error.URLError as error: - raise Req_Error((hasattr(error, "read") and error.read().decode("utf-8")) or error.reason) + raise Req_Error((hasattr(error, "read") and error.read().decode("utf-8")) or str(error.reason)) return f if python3: From 44f006cb0c70364b4a0c9d3580b490a5845b8f77 Mon Sep 17 00:00:00 2001 From: easyfrog Date: Mon, 21 Nov 2016 01:53:05 +0800 Subject: [PATCH 02/12] Change the way types are shown in completions --- tern.py | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/tern.py b/tern.py index 8f77c4f..1783291 100644 --- a/tern.py +++ b/tern.py @@ -354,16 +354,21 @@ def report_error(message, project): project.disabled = True def completion_icon(type): - if type is None or type == "?": return " (?)" - if type.startswith("fn("): return " (fn)" - if type.startswith("["): return " ([])" - if type == "number": return " (num)" - if type == "string": return " (str)" - if type == "bool": return " (bool)" - return " (obj)" - -def fn_completion_icon(arguments): - return " (fn/"+str(len(arguments))+")" + if type is None or type == "?": return "\t? " + if type.startswith("fn("): return "\tfn " + if type.startswith("["): return "\t[] " + if type == "number": return "\tnum " + if type == "string": return "\tstr " + if type == "bool": return "\tbool " + return "\t{} " + +def fn_completion_icon(arguments, retval): + # return " (fn/"+str(len(arguments))+")" + ret = "" + if retval is not None: + ret = retval + + return "(" + ", ".join(arguments) + ")" + ret + ("\tfn ") # create auto complete string from list arguments def create_arg_str(arguments): @@ -421,14 +426,22 @@ def ensure_completions_cached(pfile, view): for rec in data["completions"]: rec_name = rec.get('name').replace('$', '\\$') rec_type = rec.get("type", None) - if arg_completion_enabled and completion_icon(rec_type) == " (fn)": + if arg_completion_enabled and rec_type is not None and rec_type.startswith("fn("): + retval = parse_function_type(rec).get('retval') + + if retval is None or retval == "()": + retval = "" + elif retval.startswith("{"): + retval = "{}" + elif retval.startswith("["): + retval = "[]" + + if retval != "": + retval = " -> " + retval + arguments = get_arguments(rec_type) fn_name = rec_name + "(" + create_arg_str(arguments) + ")" - completions.append((rec.get("name") + fn_completion_icon(arguments), fn_name)) - - for i in range(len(arguments) - 1, -1, -1): - fn_name = rec_name + "(" + create_arg_str(arguments[0:i]) + ")" - completions_arity.append((rec.get("name") + fn_completion_icon(arguments[0:i]), fn_name)) + completions.append((rec.get("name") + fn_completion_icon(arguments, retval), fn_name)) else: completions.append((rec.get("name") + completion_icon(rec_type), rec_name)) From b3c1b3a7ba2fc88d034998e866a37b98c3d64bb8 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 23 Nov 2016 10:05:08 +0100 Subject: [PATCH 03/12] Don't treat unreadable HTTP errors as Req_Errors So that the code that catches them doesn't assume they are server responses. --- tern.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tern.py b/tern.py index 1783291..141b94f 100644 --- a/tern.py +++ b/tern.py @@ -264,7 +264,10 @@ def f(port, doc): req = opener.open("http://" + localhost + ":" + str(port) + "/", json.dumps(doc).encode("utf-8"), 1) return json.loads(req.read().decode("utf-8")) except urllib.error.URLError as error: - raise Req_Error((hasattr(error, "read") and error.read().decode("utf-8")) or str(error.reason)) + if hasattr(error, "read"): + raise Req_Error(error.read().decode("utf-8")) + else: + raise error return f if python3: From 2e5fd688c31df1d95820bebcae8213dbdb6572cb Mon Sep 17 00:00:00 2001 From: Nick Whyte Date: Sun, 28 May 2017 09:20:43 +1000 Subject: [PATCH 04/12] Add tern_inhibit_word_completions config option --- README.md | 3 +++ Tern.sublime-settings | 5 +++-- tern.py | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4c973f0..7a507c1 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,9 @@ plugins for a project. See the [Tern docs][docs] for details. [docs]: http://ternjs.net/doc/manual.html#configuration +`tern_inhibit_word_completions` (boolean, default to false) +If true, Prevents Sublime Text from adding its word completions to the completion list after all plugins have been processed. This consists of any word in the current document that is longer than 3 characters. + ### Automatically Showing Completions Add `{"selector": "source.js", "characters": "."}` to your diff --git a/Tern.sublime-settings b/Tern.sublime-settings index 4dc2552..6d997e0 100644 --- a/Tern.sublime-settings +++ b/Tern.sublime-settings @@ -4,5 +4,6 @@ "tern_argument_completion": false, // Used to get auto completion for unsaved buffers // By default, this folder is inside Packages/tern_for_sublime/ - "tern_default_project_dir": "default_project_dir" -} \ No newline at end of file + "tern_default_project_dir": "default_project_dir", + "tern_inhibit_word_completions": false +} diff --git a/tern.py b/tern.py index 141b94f..1c6e785 100644 --- a/tern.py +++ b/tern.py @@ -66,8 +66,12 @@ def on_query_completions(self, view, prefix, _locations): if not fresh: completions = [c for c in completions if c[1].startswith(prefix)] - return completions + flags = 0; + if get_setting("tern_inhibit_word_completions", False): + flags |= sublime.INHIBIT_WORD_COMPLETIONS + + return (completions, flags) class ProjectFile(object): def __init__(self, name, view, project): From b984dbc20020cfdbd7bd100a74caeb5dabfc7608 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 23 Aug 2017 10:47:52 +0200 Subject: [PATCH 05/12] Allow completions inside strings Issue #168 --- tern.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tern.py b/tern.py index 1c6e785..5447573 100644 --- a/tern.py +++ b/tern.py @@ -55,7 +55,6 @@ def on_selection_modified_async(self, view): def on_query_completions(self, view, prefix, _locations): sel = sel_start(view.sel()[0]) - if view.score_selector(sel, 'string.quoted') > 0: return None if view.score_selector(sel, 'comment') > 0: return None pfile = get_pfile(view) From 711fdb9eb81197ea807895062322d3a8b944c160 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Thu, 31 Aug 2017 13:28:16 +0200 Subject: [PATCH 06/12] Fix references to undefined variable Issue #169 --- tern.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tern.py b/tern.py index 5447573..0e15717 100644 --- a/tern.py +++ b/tern.py @@ -590,12 +590,12 @@ def run(self, edit, **args): class TernDisableProject(sublime_plugin.TextCommand): def run(self, edit, **args): - pfile = get_pfile(view) + pfile = get_pfile(self.view) pfile.project.disabled = False class TernEnableProject(sublime_plugin.TextCommand): def run(self, edit, **args): - pfile = get_pfile(view) + pfile = get_pfile(self.view) pfile.project.disabled = True # fetch a certain setting from the package settings file and if it doesn't exist check the From bd68869b4455141616d19aa68861dd3150ea4b5e Mon Sep 17 00:00:00 2001 From: Fedor Borshev Date: Thu, 14 Sep 2017 13:56:10 +0300 Subject: [PATCH 07/12] Fixed app running I don't know how it managed to work before, because on my system i always have `tern_command` as a string, that may not be concatenated with a list. --- tern.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tern.py b/tern.py index 0e15717..e691832 100644 --- a/tern.py +++ b/tern.py @@ -174,6 +174,10 @@ def start_server(project): if platform.system() == "Darwin": env = os.environ.copy() env["PATH"] += ":/usr/local/bin" + + if not isinstance(tern_command, list): + tern_command = [tern_command] + proc = subprocess.Popen(tern_command + tern_arguments, cwd=project.dir, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=windows) From a6590346ba5469c3cb3f79f76f63ff1e57412eaa Mon Sep 17 00:00:00 2001 From: Abe Park Date: Thu, 14 Sep 2017 13:21:13 -0700 Subject: [PATCH 08/12] Issue 174 - fix tern_command variable --- tern.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tern.py b/tern.py index e691832..ac66129 100644 --- a/tern.py +++ b/tern.py @@ -168,16 +168,17 @@ def server_port(project, ignored=None): return (started, False) def start_server(project): + global tern_command if not tern_command: return None if time.time() - project.last_failed < 30: return None env = None if platform.system() == "Darwin": env = os.environ.copy() env["PATH"] += ":/usr/local/bin" - + if not isinstance(tern_command, list): tern_command = [tern_command] - + proc = subprocess.Popen(tern_command + tern_arguments, cwd=project.dir, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=windows) From 91a27a39b1b0a33a9043aa685e1ee48c64a58274 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Mon, 27 Nov 2017 12:21:23 +0100 Subject: [PATCH 09/12] Note lack of maintenance in readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 7a507c1..91aeb16 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ This is a [Sublime Text][st] (version 2 and 3) package that provides [st]: http://www.sublimetext.com/ [tern]: http://ternjs.net +**NOTE**: This project is not being actively maintained right now. If +you'd be interested in becoming a maintainer, write me or open an +issue. + In JavaScript files, the package will handle autocompletion. The following keys will be bound (in JavaScript files): From ce046965d3209cb20e42c96b0d3e5e0256188a7b Mon Sep 17 00:00:00 2001 From: othree Date: Thu, 24 Jan 2019 09:45:41 +0800 Subject: [PATCH 10/12] Do not inset extra quote if there is already one exists --- tern.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tern.py b/tern.py index ac66129..8e292ed 100644 --- a/tern.py +++ b/tern.py @@ -54,6 +54,25 @@ def on_selection_modified_async(self, view): on_selection_modified(view) def on_query_completions(self, view, prefix, _locations): + QUOTES = ("\"", "'") + + loc = _locations[0] + prevCh = view.substr(loc - len(prefix) - 1) + nextCh = view.substr(loc) + prevQuote = prevCh in QUOTES and prevCh + nextQuote = nextCh in QUOTES and nextCh == prevQuote and nextCh + + def postfix(c): + (display, word) = c + if prevQuote and c[1][0:1] in QUOTES: + word = word[1:] + display = display[1:] + if nextQuote and c[1][-1] in QUOTES: + word = word[0:-1] + display = display.replace("%s%s" % (word, nextQuote), word, 1) + + return (display, word) + sel = sel_start(view.sel()[0]) if view.score_selector(sel, 'comment') > 0: return None @@ -66,6 +85,8 @@ def on_query_completions(self, view, prefix, _locations): if not fresh: completions = [c for c in completions if c[1].startswith(prefix)] + completions = [postfix(c) for c in completions] + flags = 0; if get_setting("tern_inhibit_word_completions", False): flags |= sublime.INHIBIT_WORD_COMPLETIONS From f1977747a622a6823a88725dbc9380d4569aade9 Mon Sep 17 00:00:00 2001 From: Amal Jose <31209960+amaljose02@users.noreply.github.com> Date: Wed, 16 Oct 2019 14:42:16 +0530 Subject: [PATCH 11/12] fixed error in the link to the repo in Readme file link to clone the repo was incorrect. Updated the link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 91aeb16..9f8ed12 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Check out the code in this repository into a subdirectory of your Sublime Text's `Packages` directory. cd /path/to/sublime-text-N/Packages - git clone git://github.com/ternjs/tern_for_sublime.git + git clone https://github.com/ternjs/tern_for_sublime.git Next, make sure [node.js][node] and [npm][npm] are installed (Tern is a JavaScript program), and install the depedencies of the package. From 85a48b3dffc27bcca0bd679b2b1141060c81a6fd Mon Sep 17 00:00:00 2001 From: othree Date: Thu, 28 Nov 2019 16:47:41 +0800 Subject: [PATCH 12/12] Fix #171, postfix path when autocomplete module path --- tern.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/tern.py b/tern.py index 8e292ed..eaed298 100644 --- a/tern.py +++ b/tern.py @@ -62,14 +62,35 @@ def on_query_completions(self, view, prefix, _locations): prevQuote = prevCh in QUOTES and prevCh nextQuote = nextCh in QUOTES and nextCh == prevQuote and nextCh - def postfix(c): + lineBeforePos = view.substr(sublime.Region(view.line(loc).begin(), loc)) + pathPrefix = lineBeforePos.rsplit(' ', 1)[0] + + pathFirstCh = pathPrefix[0] + pathFirstQuote = pathFirstCh in QUOTES and pathFirstCh + pathLastQuote = nextCh in QUOTES and nextCh == pathFirstQuote and nextCh + + def postfixQuotes(c): (display, word) = c if prevQuote and c[1][0:1] in QUOTES: word = word[1:] display = display[1:] - if nextQuote and c[1][-1] in QUOTES: - word = word[0:-1] - display = display.replace("%s%s" % (word, nextQuote), word, 1) + + if nextQuote and c[1][-1] in QUOTES: + word = word[0:-1] + display = display.replace("%s%s" % (word, nextQuote), word, 1) + + return (display, word) + + def postfixPathes(c): + (display, word) = c + + if word.startswith(pathPrefix): + word = prefix + word[len(pathPrefix):] + display = prefix + display[len(pathPrefix):] + + if pathLastQuote and c[1][-1] in QUOTES: + word = word[0:-1] + display = display.replace("%s%s" % (word, pathLastQuote), word, 1) return (display, word) @@ -85,7 +106,8 @@ def postfix(c): if not fresh: completions = [c for c in completions if c[1].startswith(prefix)] - completions = [postfix(c) for c in completions] + completions = [postfixQuotes(c) for c in completions] + completions = [postfixPathes(c) for c in completions] flags = 0; if get_setting("tern_inhibit_word_completions", False):