Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements #47

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,32 @@ GotoDocumentation allows you to edit the url that opens by editing the settings.
"python": {
// the command to be executed
"command": ["python", "-m", "pydoc", "%(query)s"],
// a regex to determine if this was an invalid response from the console
// prints command output. optional. true by default. if your command starts
// a standalone program and you are not interested in the output, you may
// want to set this to false.
"display_output": true,
// a regex to determine if this was an invalid response from the console. optional.
"failTest": ".*no Python documentation found for.*",
// regex to select something from the valid response
// regex to select something from the valid response. optional.
"changeMatch": "(Related help topics)",
// regex to replace the matched result
// regex to replace the matched result. optional.
"changeWith": "-------\n\\1",
// fallback url: if failTest returns true this will be used
// fallback url: if failTest returns true this will be used. optional.
"url": "http://docs.python.org/3/search.html?q=%(query)s"
}
},

// we can also have multiple docs for a single scope. in that case we have to use
// an array of JSON objects like below. "title" is required.
"php": [
{
"title": "PHP Manual",
"url": "http://php.net/manual-lookup.php?pattern=%(query)s"
},
{
"title": "PHP CLI Helper",
"command": ["php", "--rf", "%(query)s"]
}
]
},
// if we have no docs for the current scope
// we will try using the fallback one,
Expand All @@ -83,6 +100,10 @@ GotoDocumentation allows you to edit the url that opens by editing the settings.
```
The change replace is done with the [`re.sub`](https://docs.python.org/2/library/re.html#re.sub) method

If you define multiple docs for a scope, a quick panel will be
populated with the defined options and user will be able to pick one of
them.

## How to get the scope
To get the scope for a specific place open your sublime console with `` ctrl + ` `` and paste in this command
`view.scope_name(view.sel()[0].begin()).rpartition('.')[2].strip()`
Expand Down
60 changes: 40 additions & 20 deletions goto_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ class GotoDocumentationCommand(sublime_plugin.TextCommand):
"""
Search the selected text or the current word
"""
def run(self, edit):
def run(self, edit, query=None, scope=None, index=None):
if query is not None and scope is not None:
self.open_doc(query, scope, index)
return

# grab the word or the selection from the view
for region in self.view.sel():
location = False
Expand All @@ -62,12 +66,12 @@ def run(self, edit):
location = region

if location and not location.empty():
q = self.view.substr(location)
query = self.view.substr(location)
scope = self.view.scope_name(location.begin()).rpartition('.')[2].strip()

self.open_doc(q, scope)
self.open_doc(query, scope)

def open_doc(self, query, scope):
def open_doc(self, query, scope, index=None):

settings = sublime.load_settings("goto_documentation.sublime-settings")

Expand All @@ -93,11 +97,14 @@ def open_doc(self, query, scope):
# build the url and open it
doc = docs[tscope]

if type(doc) is list and index is not None:
doc = doc[index]

# if it is a dict we must have:
# - a command to run
# - a regex to check against
# - an optional fallback url
if type(doc) is dict:
if type(doc) is dict and 'command' in doc:
# build the command
command = [x%{'query': query, 'scope': scope} for x in doc['command']]

Expand All @@ -109,32 +116,45 @@ def open_doc(self, query, scope):

stdout = '\n'.join(stdout)

if 'display_output' in doc and doc['display_output'] is False:
return

# match the result agains the regex
reg = re.compile(doc['failTest'])
if reg.match(stdout):
# use the fallback url
if 'url' in doc:
fullUrl = doc['url']%{'query': query, 'scope': scope}
webbrowser.open(fullUrl)
else:
self.show_status("No docs available for the current word !")
if 'failTest' in doc:
reg = re.compile(doc['failTest'])
if reg.match(stdout):
# use the fallback url
if 'url' in doc:
fullUrl = doc['url']%{'query': query, 'scope': scope}
webbrowser.open(fullUrl)
else:
self.show_status("No docs available for the current word !")

return

else:
# regex to change something before it's sent to the panel
if 'changeMatch' in doc and 'changeWith' in doc:
stdout = re.sub(doc['changeMatch'], doc['changeWith'], stdout)
# regex to change something before it's sent to the panel
if 'changeMatch' in doc and 'changeWith' in doc:
stdout = re.sub(doc['changeMatch'], doc['changeWith'], stdout)

# we have a valid result from console
# so we place it in the output panel
self.panel(stdout)

# we have a valid result from console
# so we place it in the output panel
self.panel(stdout)
elif type(doc) is list:
def on_done(index):
if index >= 0:
self.open_doc(query, scope, index)

self.view.window().show_quick_panel([item['title'] for item in doc], on_done)

else:
if not doc:
self.show_status("This scope is disabled !")
return

if type(doc) is dict:
doc = doc['url']

# we have an url so we build and open it
fullUrl = doc%{'query': query, 'scope': scope}
webbrowser.open(fullUrl)
Expand Down
31 changes: 27 additions & 4 deletions goto_documentation.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,44 @@
// - %(scope)s the current scope
"css": "http://devdocs.io/#q=%(scope)s+%(query)s",

// google search + I'm feeling lucky...
"terraform": "http://www.google.com/search?hl=en&btnI=1&oq=%(query)s+site%%3Aterraform.io&q=%(query)s+site%%3Aterraform.io",

// ansible docs as google first find with site:docs.ansible.com
"ansible": "http://www.google.com/search?hl=en&btnI=1&oq=%(query)s+site%%3Adocs.ansible.com&q=%(query)s+site%%3Adocs.ansible.com",

// we can also have an object to
// run a command for finding docs
// inside the command you can use the same placeholders
"python": {
// the command to be executed
"command": ["python", "-m", "pydoc", "%(query)s"],
// a regex to determine if this was an invalid response from the console
// prints command output. optional. true by default. if your command starts
// a standalone program and you are not interested in the output, you may
// want to set this to false.
"display_output": true,
// a regex to determine if this was an invalid response from the console. optional.
"failTest": ".*no Python documentation found for.*",
// regex to select something from the valid response
// regex to select something from the valid response. optional.
"changeMatch": "(Related help topics)",
// regex to replace the matched result
// regex to replace the matched result. optional.
"changeWith": "-------\n\\1",
// fallback url: if failTest returns true this will be used
// fallback url: if failTest returns true this will be used. optional.
"url": "http://docs.python.org/3/search.html?q=%(query)s"
}

// we can also have multiple docs for a single scope. in that case we have to use
// an array of JSON objects like below. "title" is required.
// "php": [
// {
// "title": "PHP Manual",
// "url": "http://php.net/manual-lookup.php?pattern=%(query)s"
// },
// {
// "title": "PHP CLI Helper",
// "command": ["php", "--rf", "%(query)s"]
// }
// ]
},
// if we have no docs for the current scope
// we will try using the fallback one,
Expand Down