Permalink
Browse files

Implement describe functionality.

  • Loading branch information...
1 parent f5eaff8 commit d8a6ac4e24b79850c188f7456db34c5e41412d20 @tansongyang tansongyang committed with marijnh Oct 18, 2015
Showing with 66 additions and 0 deletions.
  1. +4 −0 Default.sublime-commands
  2. +7 −0 Default.sublime-keymap
  3. +4 −0 Main.sublime-menu
  4. +3 −0 README.md
  5. +16 −0 tern.py
  6. +32 −0 utils/renderer.py
@@ -10,5 +10,9 @@
{
"caption": "tern_for_sublime: Select Variable",
"command": "tern_select_variable"
+ },
+ {
+ "caption": "tern_for_sublime: Describe",
+ "command": "tern_describe"
}
]
@@ -19,5 +19,12 @@
"context": [
{"key": "selector", "operand": "source.js"}
]
+ },
+ {
+ "keys": ["alt+o"],
+ "command": "tern_describe",
+ "context": [
+ {"key": "selector", "operand": "source.js"}
+ ]
}
]
View
@@ -19,6 +19,10 @@
{
"caption": "Select all references",
"command": "tern_select_variable"
+ },
+ {
+ "caption": "Describe",
+ "command": "tern_describe"
}
]
}
View
@@ -23,6 +23,9 @@ Jump back to where you were when executing the previous `alt+.` command.
When on a variable, select all references to that variable in the
current file.
+`alt+o`
+Show quick documentation for the thing that the cursor is pointing at. Documentation includes the type, a description (if available), and documentation url (if available).
+
## Installation
Check out the code in this repository into a subdirectory of your
View
@@ -277,6 +277,11 @@ def view_js_text(view):
return text
def run_command(view, query, pos=None, fragments=True, silent=False):
+ """Run the query on the Tern server.
+
+ See default queries at http://ternjs.net/doc/manual.html#protocol.
+ """
+
pfile = get_pfile(view)
if pfile is None or pfile.project.disabled: return
@@ -553,6 +558,17 @@ def run(self, edit, **args):
self.view.sel().clear()
for r in regions: self.view.sel().add(r)
+
+class TernDescribe(sublime_plugin.TextCommand):
+ def run(self, edit, **args):
+ data = run_command(self.view, {"type": "documentation"})
+ if data is None:
+ return
+ renderer.render_description(get_pfile(self.view), self.view,
+ data["type"], data.get("doc", None),
+ data.get("url", None))
+
+
# fetch a certain setting from the package settings file and if it doesn't exist check the
# Preferences.sublime-settings file for backwards compatibility.
def get_setting(key, default):
View
@@ -1,6 +1,7 @@
# encoding=utf8
import abc
+import cgi
import textwrap
import sublime
@@ -85,6 +86,30 @@ def get_html_message_from_ftype(ftype, argpos):
return template.format(**template_data)
+
+def get_description_message(useHTML, type, doc=None, url=None):
+ """Get the message to display for Describe commands.
+
+ If useHTML is True, the message will be formatted with HTML tags.
+ """
+
+ message = type
+ if useHTML:
+ message = "<strong>[{type}]</strong>".format(type=message)
+ if doc is not None:
+ if useHTML:
+ message += "" + cgi.escape(doc)
+ else:
+ message += "\n\n" + textwrap.fill(doc, width=79)
+ if url is not None:
+ message += " "
+ if useHTML:
+ message += '<a href="{url}">[docs]</a>'.format(url=url)
+ else:
+ message += "\n\n" + url
+ return message
+
+
def maybe(fn):
def maybe_fn(arg):
return fn(arg) if arg else ''
@@ -133,6 +158,13 @@ def render_arghints(self, pfile, view, ftype, argpos):
self._render_impl(pfile, view, message)
pfile.showing_arguments = True
+ def render_description(self, pfile, view, type, doc=None, url=None):
+ """Render description."""
+
+ message = get_description_message(self.useHTML, type, doc, url)
+ self._render_impl(pfile, view, message)
+ pfile.showing_arguments = True
+
def clean(self, pfile, view):
"""Clean rendered content."""

0 comments on commit d8a6ac4

Please sign in to comment.