From 6b37a67d91b9960186eb4cf344d4288dfbb517e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Wanzenb=C3=B6ck?= Date: Fri, 27 May 2016 23:27:47 +0200 Subject: [PATCH] Add a provider for hyperclick This adds a provider for hyperclick similar to the find-definition method. Excludes some words based on the editors scope choice (e.g. let language-rust do the hard work). --- lib/racer.coffee | 66 ++++++++++++++++++++++++++++++++++-------------- package.json | 5 ++++ 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/lib/racer.coffee b/lib/racer.coffee index 535a657..dc23ad3 100644 --- a/lib/racer.coffee +++ b/lib/racer.coffee @@ -36,6 +36,8 @@ module.exports = # members racerProvider: null subscriptions: null + maybeIdentifier: /^[$0-9\w]+$/ + invalidScopes: ["core", "keyword", "comment", "language", "quoted", "modifier", "storage.type.rust"] activate: (state) -> @subscriptions = new CompositeDisposable @@ -53,6 +55,46 @@ module.exports = provideAutocompletion: -> @getRacerProvider() + provideHyperclick: -> + return { + getSuggestionForWord: (textEditor, text, range) => + grammar = textEditor.getGrammar() + if !grammar or grammar.name != 'Rust' or textEditor.hasMultipleCursors() + return + + scopeDescriptor = textEditor.scopeDescriptorForBufferPosition(range.start) + if not (text.match @maybeIdentifier) or @isNonIdentifier(scopeDescriptor) + return + + return { + range: range + callback: => + @getRacerProvider().racerClient.check_definition(textEditor, range.start.row, range.start.column, @goToDefinition) + } + } + + isNonIdentifier: (scopeDescriptor) -> + return @invalidScopes.some((invalid) -> scopeDescriptor.getScopesArray().some((scope) -> (scope.indexOf invalid) > -1)) + + goToDefinition: (defs) -> + return if _.isEmpty(defs) + def = defs[0] + + textEditors = atom.workspace.getTextEditors() + textEditor = _.find(textEditors, (te) => te.getPath() == def.filePath) + if textEditor? + pane = atom.workspace.paneForItem(textEditor) + pane.activate() + pane.activateItem(textEditor) + textEditor.setCursorBufferPosition([def.line-1, def.column]) + else + newEditorPosition = atom.config.get 'racer.show' + options = {initialLine: def.line-1, initialColumn: def.column} + options.split = newEditorPosition.toLowerCase() if newEditorPosition != 'New' + atom.workspace.open(def.filePath, options).then((te) => + te.scrollToCursorPosition() + ) + deactivate: -> @racerProvider?.dispose() @racerProvider = null @@ -68,22 +110,8 @@ module.exports = return cursorPosition = textEditor.getCursorBufferPosition() - @getRacerProvider().racerClient.check_definition(textEditor, cursorPosition.row, cursorPosition.column, (defs) => - return if _.isEmpty(defs) - def = defs[0] - - textEditors = atom.workspace.getTextEditors() - textEditor = _.find(textEditors, (te) => te.getPath() == def.filePath) - if textEditor? - pane = atom.workspace.paneForItem(textEditor) - pane.activate() - pane.activateItem(textEditor) - textEditor.setCursorBufferPosition([def.line-1, def.column]) - else - newEditorPosition = atom.config.get 'racer.show' - options = {initialLine: def.line-1, initialColumn: def.column} - options.split = newEditorPosition.toLowerCase() if newEditorPosition != 'New' - atom.workspace.open(def.filePath, options).then((te) => - te.scrollToCursorPosition() - ) - ) + scopeDescriptor = textEditor.scopeDescriptorForBufferPosition(cursorPosition) + if @isNonIdentifier(scopeDescriptor) + e.abortKeyBinding() + return + @getRacerProvider().racerClient.check_definition(textEditor, cursorPosition.row, cursorPosition.column, @goToDefinition) diff --git a/package.json b/package.json index aa90afc..b510050 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,11 @@ "temp": "~0.8.1" }, "providedServices": { + "hyperclick.provider": { + "versions": { + "0.0.0": "provideHyperclick" + } + }, "autocomplete.provider": { "description": "Intelligent Rust code completion", "versions": {