-
Notifications
You must be signed in to change notification settings - Fork 285
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
Add workspace/symbol support using ctags #507
Open
gatesn
wants to merge
7
commits into
develop
Choose a base branch
from
ngates/workspace-symbols
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6dee330
Add workspace symbol support
gatesn 60b50d1
Add workspace symbol support
gatesn d9f3ed4
Add workspace symbol support
gatesn c2267ff
Test windows ctags
gatesn dafd507
Test windows ctags
gatesn 8a3f960
Test windows ctags
gatesn 3e1d487
Test windows ctags
gatesn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
# Copyright 2017 Palantir Technologies, Inc. | ||
import io | ||
import logging | ||
import os | ||
import re | ||
import subprocess | ||
|
||
|
||
from pyls import hookimpl, uris | ||
from pyls.lsp import SymbolKind | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
DEFAULT_TAG_FILE = "${workspaceFolder}/.vscode/tags" | ||
DEFAULT_CTAGS_EXE = "ctags" | ||
|
||
TAG_RE = re.compile(( | ||
r'(?P<name>\w+)\t' | ||
r'(?P<file>.*)\t' | ||
r'/\^(?P<code>.*)\$/;"\t' | ||
r'kind:(?P<type>\w+)\t' | ||
r'line:(?P<line>\d+)$' | ||
)) | ||
|
||
CTAG_OPTIONS = [ | ||
"--tag-relative=yes", | ||
"--exclude=.git", | ||
"--exclude=env", | ||
"--exclude=log", | ||
"--exclude=tmp", | ||
"--exclude=doc", | ||
"--exclude=deps", | ||
"--exclude=node_modules", | ||
"--exclude=.vscode", | ||
"--exclude=public/assets", | ||
"--exclude=*.git*", | ||
"--exclude=*.pyc", | ||
"--exclude=*.pyo", | ||
"--exclude=.DS_Store", | ||
"--exclude=**/*.jar", | ||
"--exclude=**/*.class", | ||
"--exclude=**/.idea/", | ||
"--exclude=build", | ||
"--exclude=Builds", | ||
"--exclude=doc", | ||
"--fields=Knz", | ||
"--extra=+f", | ||
] | ||
|
||
CTAG_SYMBOL_MAPPING = { | ||
"array": SymbolKind.Array, | ||
"boolean": SymbolKind.Boolean, | ||
"class": SymbolKind.Class, | ||
"classes": SymbolKind.Class, | ||
"constant": SymbolKind.Constant, | ||
"constants": SymbolKind.Constant, | ||
"constructor": SymbolKind.Constructor, | ||
"enum": SymbolKind.Enum, | ||
"enums": SymbolKind.Enum, | ||
"enumeration": SymbolKind.Enum, | ||
"enumerations": SymbolKind.Enum, | ||
"field": SymbolKind.Field, | ||
"fields": SymbolKind.Field, | ||
"file": SymbolKind.File, | ||
"files": SymbolKind.File, | ||
"function": SymbolKind.Function, | ||
"functions": SymbolKind.Function, | ||
"member": SymbolKind.Function, | ||
"interface": SymbolKind.Interface, | ||
"interfaces": SymbolKind.Interface, | ||
"key": SymbolKind.Key, | ||
"keys": SymbolKind.Key, | ||
"method": SymbolKind.Method, | ||
"methods": SymbolKind.Method, | ||
"module": SymbolKind.Module, | ||
"modules": SymbolKind.Module, | ||
"namespace": SymbolKind.Namespace, | ||
"namespaces": SymbolKind.Namespace, | ||
"number": SymbolKind.Number, | ||
"numbers": SymbolKind.Number, | ||
"null": SymbolKind.Null, | ||
"object": SymbolKind.Object, | ||
"package": SymbolKind.Package, | ||
"packages": SymbolKind.Package, | ||
"property": SymbolKind.Property, | ||
"properties": SymbolKind.Property, | ||
"objects": SymbolKind.Object, | ||
"string": SymbolKind.String, | ||
"variable": SymbolKind.Variable, | ||
"variables": SymbolKind.Variable, | ||
"projects": SymbolKind.Package, | ||
"defines": SymbolKind.Module, | ||
"labels": SymbolKind.Interface, | ||
"macros": SymbolKind.Function, | ||
"types (structs and records)": SymbolKind.Class, | ||
"subroutine": SymbolKind.Method, | ||
"subroutines": SymbolKind.Method, | ||
"types": SymbolKind.Class, | ||
"programs": SymbolKind.Class, | ||
"Object\'s method": SymbolKind.Method, | ||
"Module or functor": SymbolKind.Module, | ||
"Global variable": SymbolKind.Variable, | ||
"Type name": SymbolKind.Class, | ||
"A function": SymbolKind.Function, | ||
"A constructor": SymbolKind.Constructor, | ||
"An exception": SymbolKind.Class, | ||
"A \'structure\' field": SymbolKind.Field, | ||
"procedure": SymbolKind.Function, | ||
"procedures": SymbolKind.Function, | ||
"constant definitions": SymbolKind.Constant, | ||
"javascript functions": SymbolKind.Function, | ||
"singleton methods": SymbolKind.Method, | ||
} | ||
|
||
|
||
class CtagMode(object): | ||
NONE = "none" | ||
APPEND = "append" | ||
REBUILD = "rebuild" | ||
|
||
|
||
DEFAULT_ON_START_MODE = CtagMode.REBUILD | ||
DEFAULT_ON_SAVE_MODE = CtagMode.APPEND | ||
|
||
|
||
class CtagsPlugin(object): | ||
|
||
def __init__(self): | ||
self._started = False | ||
self._workspace = None | ||
|
||
@hookimpl | ||
def pyls_document_did_open(self, config, workspace): | ||
"""Since initial settings are sent after initialization, we use didOpen as the hook instead.""" | ||
if self._started: | ||
return | ||
self._started = True | ||
self._workspace = workspace | ||
|
||
settings = config.plugin_settings('ctags') | ||
ctags_exe = _ctags_exe(settings) | ||
|
||
for tag_file in settings.get('tagFiles', []): | ||
mode = tag_file.get('onStart', DEFAULT_ON_START_MODE) | ||
|
||
if mode == CtagMode.NONE: | ||
log.debug("Skipping tag file with onStart mode NONE: %s", tag_file) | ||
continue | ||
|
||
tag_file_path = self._format_path(tag_file['filePath']) | ||
tags_dir = self._format_path(tag_file['directory']) | ||
|
||
execute(ctags_exe, tag_file_path, tags_dir, mode == CtagMode.APPEND) | ||
|
||
@hookimpl | ||
def pyls_document_did_save(self, config, document): | ||
settings = config.plugin_settings('ctags') | ||
ctags_exe = _ctags_exe(settings) | ||
|
||
for tag_file in settings.get('tagFiles', []): | ||
mode = tag_file.get('onSave', DEFAULT_ON_SAVE_MODE) | ||
|
||
if mode == CtagMode.NONE: | ||
log.debug("Skipping tag file with onSave mode NONE: %s", tag_file) | ||
continue | ||
|
||
tag_file_path = self._format_path(tag_file['filePath']) | ||
tags_dir = self._format_path(tag_file['directory']) | ||
|
||
if not os.path.normpath(document.path).startswith(os.path.normpath(tags_dir)): | ||
log.debug("Skipping onSave tag generation since %s is not in %s", tag_file_path, tags_dir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug message should use document.path instead of tag_file_path |
||
continue | ||
|
||
execute(ctags_exe, tag_file_path, tags_dir, mode == CtagMode.APPEND) | ||
|
||
@hookimpl | ||
def pyls_workspace_symbols(self, config, query): | ||
settings = config.plugin_settings('ctags') | ||
|
||
symbols = [] | ||
for tag_file in settings.get('tagFiles', []): | ||
symbols.extend(parse_tags(self._format_path(tag_file['filePath']), query)) | ||
|
||
return symbols | ||
|
||
def _format_path(self, path): | ||
return path.format(**{"workspaceRoot": self._workspace.root_path}) | ||
|
||
|
||
def _ctags_exe(settings): | ||
# TODO(gatesn): verify ctags is installed and right version | ||
return settings.get('ctagsPath', DEFAULT_CTAGS_EXE) | ||
|
||
|
||
def execute(ctags_exe, tag_file, directory, append=False): | ||
"""Run ctags against the given directory.""" | ||
# Ensure the directory exists | ||
tag_file_dir = os.path.dirname(tag_file) | ||
if not os.path.exists(tag_file_dir): | ||
os.makedirs(tag_file_dir) | ||
|
||
cmd = [ctags_exe, '-f', tag_file, '--languages=Python', '-R'] + CTAG_OPTIONS | ||
if append: | ||
cmd.append('--append') | ||
cmd.append(directory) | ||
|
||
log.info("Executing exuberant ctags: %s", cmd) | ||
log.info("ctags: %s", subprocess.check_output(cmd)) | ||
|
||
|
||
def parse_tags(tag_file, query): | ||
if not os.path.exists(tag_file): | ||
return | ||
|
||
with io.open(tag_file, 'rb') as f: | ||
for line in f: | ||
tag = parse_tag(line.decode('utf-8', errors='ignore'), query) | ||
if tag: | ||
yield tag | ||
|
||
|
||
def parse_tag(line, query): | ||
match = TAG_RE.match(line) | ||
log.info("Got match %s from line: %s", match, line) | ||
log.info("Line: %s", line.replace('\t', '\\t').replace(' ', '\\s')) | ||
|
||
if not match: | ||
return None | ||
|
||
name = match.group('name') | ||
|
||
# TODO(gatesn): Support a fuzzy match, but for now do a naive substring match | ||
if query.lower() not in name.lower(): | ||
return None | ||
|
||
line = int(match.group('line')) - 1 | ||
|
||
return { | ||
'name': name, | ||
'kind': CTAG_SYMBOL_MAPPING.get(match.group('type'), SymbolKind.Null), | ||
'location': { | ||
'uri': uris.from_fs_path(match.group('file')), | ||
'range': { | ||
'start': {'line': line, 'character': 0}, | ||
'end': {'line': line, 'character': 0} | ||
} | ||
} | ||
} | ||
|
||
|
||
INSTANCE = CtagsPlugin() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not consider Windows line endings (\r\n). The regex will fail on Windows files. Suggest changing line to:
r'line:(?P<line>\d+)\s*$'