-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
word based highlight for unsupported languages
- Loading branch information
1 parent
a6ce555
commit 27b5122
Showing
5 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
packages/language-server/src/lib/documentHighlight/wordHighlight.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,89 @@ | ||
import { | ||
DocumentHighlight, | ||
DocumentHighlightKind, | ||
Position, | ||
Range | ||
} from 'vscode-languageserver-types'; | ||
import { Document, TagInformation } from '../documents'; | ||
|
||
export function wordHighlightForTag( | ||
document: Document, | ||
position: Position, | ||
tag: TagInformation | null, | ||
wordPattern: RegExp | ||
): DocumentHighlight[] | null { | ||
if (!tag || tag.start === tag.end) { | ||
return null; | ||
} | ||
|
||
const offset = document.offsetAt(position); | ||
|
||
const text = document.getText(); | ||
if ( | ||
offset < tag.start || | ||
offset > tag.end || | ||
// empty before and after the cursor | ||
!text.slice(offset - 1, offset + 1).trim() | ||
) { | ||
return null; | ||
} | ||
|
||
const word = wordAt(document, position, wordPattern); | ||
if (!word) { | ||
return null; | ||
} | ||
|
||
const searching = document.getText().slice(tag.start, tag.end); | ||
|
||
const highlights: DocumentHighlight[] = []; | ||
|
||
let index = 0; | ||
while (index < searching.length) { | ||
index = searching.indexOf(word, index); | ||
if (index === -1) { | ||
break; | ||
} | ||
|
||
const start = tag.start + index; | ||
highlights.push({ | ||
range: { | ||
start: document.positionAt(start), | ||
end: document.positionAt(start + word.length) | ||
}, | ||
kind: DocumentHighlightKind.Text | ||
}); | ||
|
||
index += word.length; | ||
} | ||
|
||
return highlights; | ||
} | ||
|
||
function wordAt(document: Document, position: Position, wordPattern: RegExp): string | null { | ||
const line = document | ||
.getText( | ||
Range.create(Position.create(position.line, 0), Position.create(position.line + 1, 0)) | ||
) | ||
.trimEnd(); | ||
|
||
wordPattern.lastIndex = 0; | ||
|
||
let start: number | undefined; | ||
let end: number | undefined; | ||
const matchEnd = Math.min(position.character, line.length); | ||
while (wordPattern.lastIndex < matchEnd) { | ||
const match = wordPattern.exec(line); | ||
if (!match) { | ||
break; | ||
} | ||
|
||
start = match.index; | ||
end = match.index + match[0].length; | ||
} | ||
|
||
if (start === undefined || end === undefined || end < position.character) { | ||
return null; | ||
} | ||
|
||
return line.slice(start, end); | ||
} |
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
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