-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
385 additions
and
201 deletions.
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
105 changes: 105 additions & 0 deletions
105
languageServer/src/languageHelpers/baseLanguageHelper.ts
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,105 @@ | ||
import { has } from 'lodash' | ||
import { TextDocument } from 'vscode-languageserver-textdocument' | ||
import { | ||
DocumentSymbol, | ||
Position, | ||
Location, | ||
SymbolKind | ||
} from 'vscode-languageserver/node' | ||
|
||
export interface LanguageHelper { | ||
findSymbolAtPosition(position: Position): DocumentSymbol | undefined | ||
findLocationsFor(symbol: DocumentSymbol): Location[] | ||
} | ||
|
||
export abstract class BaseLanguageHelper<RootNode> implements LanguageHelper { | ||
protected textDocument: TextDocument | ||
protected rootNode?: RootNode | ||
|
||
constructor(textDocument: TextDocument) { | ||
this.textDocument = textDocument | ||
this.rootNode = this.parse(this.getText()) | ||
} | ||
|
||
public findSymbolAtPosition(position: Position): DocumentSymbol | undefined { | ||
const cursorOffset: number = this.offsetAt(position) | ||
const symbolsAroundOffset = this.findEnclosingSymbols(cursorOffset) | ||
|
||
const symbolStack = symbolsAroundOffset.sort((a, b) => { | ||
const offA = this.offsetAt(a.range.end) - this.offsetAt(a.range.start) | ||
const offB = this.offsetAt(b.range.end) - this.offsetAt(b.range.start) | ||
|
||
return offB - offA // We want the tighter fits for last, so we can just pop them | ||
}) | ||
|
||
return [...symbolStack].pop() | ||
} | ||
|
||
public findLocationsFor(symbol: DocumentSymbol): Location[] { | ||
if (symbol.kind === SymbolKind.Property) { | ||
return this.findLocationsForPropertySymbol(symbol) | ||
} | ||
|
||
return this.findLocationsForNormalSymbol(symbol) | ||
} | ||
|
||
protected getText() { | ||
return this.textDocument.getText() | ||
} | ||
|
||
protected offsetAt(position: Position) { | ||
return this.textDocument.offsetAt(position) | ||
} | ||
|
||
protected positionAt(offset: number) { | ||
return this.textDocument.positionAt(offset) | ||
} | ||
|
||
private findLocationsForPropertySymbol(symbol: DocumentSymbol) { | ||
const propertyPath = symbol.detail | ||
const itIsHere = propertyPath && this.hasProperty(propertyPath) | ||
|
||
if (itIsHere) { | ||
const pathArray = propertyPath.split('.') | ||
const location = this.getPropertyLocation(pathArray) | ||
|
||
return location ? [location] : [] | ||
} | ||
|
||
return this.findLocationsForNormalSymbol(symbol) | ||
} | ||
|
||
private findLocationsForNormalSymbol(symbol: DocumentSymbol) { | ||
const parts = symbol.name.split(/\s/g) | ||
const txt = this.getText() | ||
|
||
const acc: Location[] = [] | ||
for (const str of parts) { | ||
const index = txt.indexOf(str) | ||
if (index <= 0) { | ||
continue | ||
} | ||
const pos = this.positionAt(index) | ||
const range = this.findSymbolAtPosition(pos)?.range | ||
|
||
if (range) { | ||
acc.push(Location.create(this.textDocument.uri, range)) | ||
} | ||
} | ||
return acc | ||
} | ||
|
||
private hasProperty(path: string) { | ||
const parsedObj = this.toJSON() | ||
|
||
return has(parsedObj, path) | ||
} | ||
|
||
protected abstract parse(source: string): RootNode | undefined | ||
protected abstract findEnclosingSymbols(offset: number): DocumentSymbol[] | ||
protected abstract getPropertyLocation( | ||
pathArray: Array<string | number> | ||
): Location | null | ||
|
||
protected abstract toJSON(): unknown | ||
} |
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,18 @@ | ||
import { TextDocument } from 'vscode-languageserver-textdocument' | ||
import { JsonHelper } from './jsonHelper' | ||
import { PlainTextHelper } from './plainTextHelper' | ||
import { YamlHelper } from './yamlHelper' | ||
|
||
export const createLanguageHelper = (textDocument: TextDocument) => { | ||
const language = textDocument.languageId | ||
|
||
if (language === 'yaml') { | ||
return new YamlHelper(textDocument) | ||
} | ||
|
||
if (language === 'json') { | ||
return new JsonHelper(textDocument) | ||
} | ||
|
||
return new PlainTextHelper(textDocument) | ||
} |
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,34 @@ | ||
import { findNodeAtLocation, getNodeValue, Node, parseTree } from 'jsonc-parser' | ||
import { DocumentSymbol, Location, Range } from 'vscode-languageserver' | ||
import { BaseLanguageHelper } from './baseLanguageHelper' | ||
|
||
export class JsonHelper extends BaseLanguageHelper<Node> { | ||
protected parse(source: string) { | ||
return parseTree(source) | ||
} | ||
|
||
protected findEnclosingSymbols(): DocumentSymbol[] { | ||
return [] | ||
} | ||
|
||
protected getPropertyLocation( | ||
pathArray: Array<string | number> | ||
): Location | null { | ||
const node = this.rootNode && findNodeAtLocation(this.rootNode, pathArray) | ||
|
||
if (!node) { | ||
return null | ||
} | ||
const nodeSrcIndex = node.offset | ||
const nodeSrcLength = node.length | ||
const nodeEnd = nodeSrcIndex + nodeSrcLength | ||
const start = this.positionAt(nodeSrcIndex) | ||
const end = this.positionAt(nodeEnd) | ||
const range = Range.create(start, end) | ||
return Location.create(this.textDocument.uri, range) | ||
} | ||
|
||
protected toJSON(): unknown { | ||
return this.rootNode ? getNodeValue(this.rootNode) : undefined | ||
} | ||
} |
Oops, something went wrong.