Skip to content

Commit

Permalink
feat: Support symbol highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
vain0x committed Jan 7, 2019
1 parent 1e7cc2d commit ac63c6b
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lsp/src/curage-server.ts
Expand Up @@ -12,13 +12,19 @@ import {
Position,
Range,
DidCloseTextDocumentParams,
DocumentSymbolParams,
DocumentSymbol,
DocumentHighlight,
DocumentHighlightKind,
TextDocumentPositionParams,
} from "vscode-languageserver-protocol"
import {
listenToLSPClient,
sendNotify,
sendResponse,
sendRequest,
} from "./communication"
import { SymbolKind } from "vscode";

interface Message {
jsonrpc: string,
Expand All @@ -45,6 +51,9 @@ export const onMessage = (message: Message) => {
// including the full text of the modified document.
change: TextDocumentSyncKind.Full,
},
// Indicate that the server can respond to
// `textDocument/documentHighlight` requests.
documentHighlightProvider: true,
},
} as InitializeResult)
break
Expand Down Expand Up @@ -76,6 +85,12 @@ export const onMessage = (message: Message) => {
const { textDocument: { uri } } = params as DidCloseTextDocumentParams
openDocuments.delete(uri)
}
case "textDocument/documentHighlight": {
const { textDocument: { uri }, position } = params as TextDocumentPositionParams
const highlights = createHighlights(uri, position)
sendResponse(id, highlights || null)
return
}
default: {
// Pass.
break
Expand Down Expand Up @@ -614,6 +629,39 @@ const documentDidOpenOrChange = (uri: string, text) => {
} as PublishDiagnosticsParams)
}

/**
* Create highlights to emphasis tokens
* same as the symbol at the specified position.
*/
const createHighlights = (uri: string, position: Position) => {
const semanticModel = openDocuments.get(uri)
if (!semanticModel) {
return
}

const symbolDefinition = hitTestSymbol(semanticModel, position)
if (!symbolDefinition) {
return
}

const highlights: DocumentHighlight[] = []
const { definition, references } = symbolDefinition

highlights.push({
kind: DocumentHighlightKind.Write,
range: definition.range,
})

for (const r of references) {
highlights.push({
kind: DocumentHighlightKind.Read,
range: r.range,
})
}

return highlights
}

export const main = () => {
listenToLSPClient()
}

0 comments on commit ac63c6b

Please sign in to comment.