From 2d0ee14b7544b2d715ffad3873449f525db3bc9e Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Fri, 30 Dec 2016 08:44:20 +0900 Subject: [PATCH] Implement DocumentSymbolProvider --- locate/locate.js | 4 ++++ ruby.js | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/locate/locate.js b/locate/locate.js index f42264648..b6fecb98c 100644 --- a/locate/locate.js +++ b/locate/locate.js @@ -17,6 +17,7 @@ function flatten(locateInfo, file, parentName) { const symbolInfo = { // TODO: parse names like 'ActiveRecord::Base' name: name, + type: type, file: file, line: posn.line, char: posn.char, @@ -37,6 +38,9 @@ module.exports = class Locate { // always: do this file now (if it's in the tree) // add lookup hooks } + listInFile(absPath) { + return _.clone(this.tree[absPath] || []); + } find(name) { // because our word pattern is designed to match symbols // things like Gem::RequestSet may request a search for ':RequestSet' diff --git a/ruby.js b/ruby.js index 6b5292001..3f50eba8c 100644 --- a/ruby.js +++ b/ruby.js @@ -1,5 +1,6 @@ "use strict"; -let vscode = require('vscode'); +const vscode = require('vscode'); +const { Location, Position, SymbolKind, SymbolInformation } = vscode; let Locate = require('./locate/locate'); let cp = require('child_process'); @@ -177,6 +178,24 @@ function activate(context) { } }; subs.push(vscode.languages.registerDefinitionProvider(['ruby', 'erb'], defProvider)); + const symbolKindTable = { + class: () => SymbolKind.Class, + module: () => SymbolKind.Module, + method: symbolInfo => symbolInfo.name === 'initialize' ? SymbolKind.Constructor : SymbolKind.Method, + classMethod: () => SymbolKind.Method, + }; + const docSymbolProvider = { + provideDocumentSymbols: (document, token) => { + return locate.listInFile(document.fileName).map(match => { + const symbolKind = symbolKindTable[match.type](match); + const parentName = match.parent ? match.parent.fullName : ''; + const uri = vscode.Uri.file(match.file); + const location = new Location(uri, new Position(match.line, match.char)); + return new SymbolInformation(match.name, symbolKind, parentName, location); + }) + } + }; + subs.push(vscode.languages.registerDocumentSymbolProvider(['ruby', 'erb'], docSymbolProvider)); } subs.push(vscode.window.onDidChangeActiveTextEditor(balanceEvent));