|
| 1 | +# Copyright 2017 Palantir Technologies, Inc. |
| 2 | +import io |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import re |
| 6 | +import subprocess |
| 7 | + |
| 8 | + |
| 9 | +from pyls import hookimpl, uris |
| 10 | +from pyls.lsp import SymbolKind |
| 11 | + |
| 12 | +log = logging.getLogger(__name__) |
| 13 | + |
| 14 | +DEFAULT_TAG_FILE = "${workspaceFolder}/.vscode/tags" |
| 15 | +DEFAULT_CTAGS_EXE = "ctags" |
| 16 | + |
| 17 | +TAG_RE = re.compile(( |
| 18 | + r'(?P<name>\w+)\t' |
| 19 | + r'(?P<file>.*)\t' |
| 20 | + r'/\^(?P<code>.*)\$/;"\t' |
| 21 | + r'kind:(?P<type>\w+)\t' |
| 22 | + r'line:(?P<line>\d+)$' |
| 23 | +)) |
| 24 | + |
| 25 | +CTAG_OPTIONS = [ |
| 26 | + "--tag-relative=yes", |
| 27 | + "--exclude=.git", |
| 28 | + "--exclude=env", |
| 29 | + "--exclude=log", |
| 30 | + "--exclude=tmp", |
| 31 | + "--exclude=doc", |
| 32 | + "--exclude=deps", |
| 33 | + "--exclude=node_modules", |
| 34 | + "--exclude=.vscode", |
| 35 | + "--exclude=public/assets", |
| 36 | + "--exclude=*.git*", |
| 37 | + "--exclude=*.pyc", |
| 38 | + "--exclude=*.pyo", |
| 39 | + "--exclude=.DS_Store", |
| 40 | + "--exclude=**/*.jar", |
| 41 | + "--exclude=**/*.class", |
| 42 | + "--exclude=**/.idea/", |
| 43 | + "--exclude=build", |
| 44 | + "--exclude=Builds", |
| 45 | + "--exclude=doc", |
| 46 | + "--fields=Knz", |
| 47 | + "--extra=+f", |
| 48 | +] |
| 49 | + |
| 50 | +CTAG_SYMBOL_MAPPING = { |
| 51 | + "array": SymbolKind.Array, |
| 52 | + "boolean": SymbolKind.Boolean, |
| 53 | + "class": SymbolKind.Class, |
| 54 | + "classes": SymbolKind.Class, |
| 55 | + "constant": SymbolKind.Constant, |
| 56 | + "constants": SymbolKind.Constant, |
| 57 | + "constructor": SymbolKind.Constructor, |
| 58 | + "enum": SymbolKind.Enum, |
| 59 | + "enums": SymbolKind.Enum, |
| 60 | + "enumeration": SymbolKind.Enum, |
| 61 | + "enumerations": SymbolKind.Enum, |
| 62 | + "field": SymbolKind.Field, |
| 63 | + "fields": SymbolKind.Field, |
| 64 | + "file": SymbolKind.File, |
| 65 | + "files": SymbolKind.File, |
| 66 | + "function": SymbolKind.Function, |
| 67 | + "functions": SymbolKind.Function, |
| 68 | + "member": SymbolKind.Function, |
| 69 | + "interface": SymbolKind.Interface, |
| 70 | + "interfaces": SymbolKind.Interface, |
| 71 | + "key": SymbolKind.Key, |
| 72 | + "keys": SymbolKind.Key, |
| 73 | + "method": SymbolKind.Method, |
| 74 | + "methods": SymbolKind.Method, |
| 75 | + "module": SymbolKind.Module, |
| 76 | + "modules": SymbolKind.Module, |
| 77 | + "namespace": SymbolKind.Namespace, |
| 78 | + "namespaces": SymbolKind.Namespace, |
| 79 | + "number": SymbolKind.Number, |
| 80 | + "numbers": SymbolKind.Number, |
| 81 | + "null": SymbolKind.Null, |
| 82 | + "object": SymbolKind.Object, |
| 83 | + "package": SymbolKind.Package, |
| 84 | + "packages": SymbolKind.Package, |
| 85 | + "property": SymbolKind.Property, |
| 86 | + "properties": SymbolKind.Property, |
| 87 | + "objects": SymbolKind.Object, |
| 88 | + "string": SymbolKind.String, |
| 89 | + "variable": SymbolKind.Variable, |
| 90 | + "variables": SymbolKind.Variable, |
| 91 | + "projects": SymbolKind.Package, |
| 92 | + "defines": SymbolKind.Module, |
| 93 | + "labels": SymbolKind.Interface, |
| 94 | + "macros": SymbolKind.Function, |
| 95 | + "types (structs and records)": SymbolKind.Class, |
| 96 | + "subroutine": SymbolKind.Method, |
| 97 | + "subroutines": SymbolKind.Method, |
| 98 | + "types": SymbolKind.Class, |
| 99 | + "programs": SymbolKind.Class, |
| 100 | + "Object\'s method": SymbolKind.Method, |
| 101 | + "Module or functor": SymbolKind.Module, |
| 102 | + "Global variable": SymbolKind.Variable, |
| 103 | + "Type name": SymbolKind.Class, |
| 104 | + "A function": SymbolKind.Function, |
| 105 | + "A constructor": SymbolKind.Constructor, |
| 106 | + "An exception": SymbolKind.Class, |
| 107 | + "A \'structure\' field": SymbolKind.Field, |
| 108 | + "procedure": SymbolKind.Function, |
| 109 | + "procedures": SymbolKind.Function, |
| 110 | + "constant definitions": SymbolKind.Constant, |
| 111 | + "javascript functions": SymbolKind.Function, |
| 112 | + "singleton methods": SymbolKind.Method, |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +class CtagMode(object): |
| 117 | + NONE = "none" |
| 118 | + APPEND = "append" |
| 119 | + REBUILD = "rebuild" |
| 120 | + |
| 121 | + |
| 122 | +DEFAULT_ON_START_MODE = CtagMode.REBUILD |
| 123 | +DEFAULT_ON_SAVE_MODE = CtagMode.APPEND |
| 124 | + |
| 125 | + |
| 126 | +class CtagsPlugin(object): |
| 127 | + |
| 128 | + def __init__(self): |
| 129 | + self._started = False |
| 130 | + self._workspace = None |
| 131 | + |
| 132 | + @hookimpl |
| 133 | + def pyls_document_did_open(self, config, workspace): |
| 134 | + """Since initial settings are sent after initialization, we use didOpen as the hook instead.""" |
| 135 | + if self._started: |
| 136 | + return |
| 137 | + self._started = True |
| 138 | + self._workspace = workspace |
| 139 | + |
| 140 | + settings = config.plugin_settings('ctags') |
| 141 | + ctags_exe = _ctags_exe(settings) |
| 142 | + |
| 143 | + for tag_file in settings.get('tagFiles', []): |
| 144 | + mode = tag_file.get('onStart', CtagMode.DEFAULT_ON_START_MODE) |
| 145 | + |
| 146 | + if mode == CtagMode.NONE: |
| 147 | + log.debug("Skipping tag file with onStart mode NONE: %s", tag_file) |
| 148 | + continue |
| 149 | + |
| 150 | + tag_file_path = self._format_path(tag_file['filePath']) |
| 151 | + tags_dir = self._format_path(tag_file['directory']) |
| 152 | + |
| 153 | + execute(ctags_exe, tag_file_path, tags_dir, mode == CtagMode.APPEND) |
| 154 | + |
| 155 | + @hookimpl |
| 156 | + def pyls_document_did_save(self, config, document): |
| 157 | + settings = config.plugin_settings('ctags') |
| 158 | + ctags_exe = _ctags_exe(settings) |
| 159 | + |
| 160 | + for tag_file in settings.get('tagFiles', []): |
| 161 | + mode = tag_file.get('onSave', CtagMode.DEFAULT_ON_SAVE_MODE) |
| 162 | + |
| 163 | + if mode == CtagMode.NONE: |
| 164 | + log.debug("Skipping tag file with onSave mode NONE: %s", tag_file) |
| 165 | + continue |
| 166 | + |
| 167 | + tag_file_path = self._format_path(tag_file['filePath']) |
| 168 | + tags_dir = self._format_path(tag_file['directory']) |
| 169 | + |
| 170 | + if not os.path.normpath(document.path).startswith(os.path.normpath(tags_dir)): |
| 171 | + log.debug("Skipping onSave tag generation since %s is not in %s", tag_file_path, tags_dir) |
| 172 | + continue |
| 173 | + |
| 174 | + execute(ctags_exe, tag_file_path, tags_dir, mode == CtagMode.APPEND) |
| 175 | + |
| 176 | + @hookimpl |
| 177 | + def pyls_workspace_symbols(self, config, query): |
| 178 | + settings = config.plugin_settings('ctags') |
| 179 | + |
| 180 | + symbols = [] |
| 181 | + for tag_file in settings.get('tagFiles', []): |
| 182 | + symbols.extend(parse_tags(self._format_path(tag_file['filePath']), query)) |
| 183 | + |
| 184 | + return symbols |
| 185 | + |
| 186 | + def _format_path(self, path): |
| 187 | + return path.format(**{"workspaceRoot": self._workspace.root_path}) |
| 188 | + |
| 189 | + |
| 190 | +def _ctags_exe(settings): |
| 191 | + # TODO(gatesn): verify ctags is installed and right version |
| 192 | + return settings.get('ctagsPath', DEFAULT_CTAGS_EXE) |
| 193 | + |
| 194 | + |
| 195 | +def execute(ctags_exe, tag_file, directory, append=False): |
| 196 | + """Run ctags against the given directory.""" |
| 197 | + # Ensure the directory exists |
| 198 | + tag_file_dir = os.path.dirname(tag_file) |
| 199 | + if not os.path.exists(tag_file_dir): |
| 200 | + os.makedirs(tag_file_dir) |
| 201 | + |
| 202 | + cmd = [ctags_exe, '-f', tag_file, '--languages=Python', '-R'] + CTAG_OPTIONS |
| 203 | + if append: |
| 204 | + cmd.append('--append') |
| 205 | + cmd.append(directory) |
| 206 | + |
| 207 | + log.info("Executing exuberant ctags: %s", cmd) |
| 208 | + log.info("ctags: %s", subprocess.check_output(cmd)) |
| 209 | + |
| 210 | + |
| 211 | +def parse_tags(tag_file, query): |
| 212 | + if not os.path.exists(tag_file): |
| 213 | + return [] |
| 214 | + |
| 215 | + with io.open(tag_file, 'rb') as f: |
| 216 | + for line in f: |
| 217 | + tag = parse_tag(line.decode('utf-8', errors='ignore'), query) |
| 218 | + if tag: |
| 219 | + yield tag |
| 220 | + |
| 221 | + |
| 222 | +def parse_tag(line, query): |
| 223 | + match = TAG_RE.match(line) |
| 224 | + if not match: |
| 225 | + return None |
| 226 | + |
| 227 | + name = match.group('name') |
| 228 | + |
| 229 | + # TODO(gatesn): Support a fuzzy match, but for now do a naive substring match |
| 230 | + if query.lower() not in name.lower(): |
| 231 | + return None |
| 232 | + |
| 233 | + line = int(match.group('line')) - 1 |
| 234 | + |
| 235 | + return { |
| 236 | + 'name': name, |
| 237 | + 'kind': CTAG_SYMBOL_MAPPING.get(match.group('type'), SymbolKind.Null), |
| 238 | + 'location': { |
| 239 | + 'uri': uris.from_fs_path(match.group('file')), |
| 240 | + 'range': { |
| 241 | + 'start': {'line': line, 'character': 0}, |
| 242 | + 'end': {'line': line, 'character': 0} |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + |
| 248 | +INSTANCE = CtagsPlugin() |
0 commit comments