-
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
9 changed files
with
382 additions
and
290 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
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 | ||
} |
Oops, something went wrong.