-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Semantic document highlight (#1408)
* ts, html, css * WIP * fast forward * general svelte blocks and tag * only same file highlights * else highlights for if block * await block * highlight in style attribute * exclude unsupported * don't filter out highlights if original is not in it function and class highlght its name * config * ts, html and css test * svelte test * format * merge two utils files * revert unnecessary changes * single config, mark as experimental * move the svelte highlight to ts using ast returned by svelte2tsx * config description * word based highlight for unsupported languages * format * ignore for svelte 5 * fix svelte 5 issues. workaround the await block issue and fixing it separately * Apply suggestions from code review Co-authored-by: Simon H <[email protected]> * prevent word-base fallback * fix nested if, if block without else if * remove experimental keep the config so people can still go back to the word base highlight * fix svelte 4 again * fix * Update packages/language-server/src/plugins/PluginHost.ts * fix the fix --------- Co-authored-by: Simon H <[email protected]> Co-authored-by: Simon Holthausen <[email protected]>
- Loading branch information
1 parent
646f2e6
commit 931dd85
Showing
20 changed files
with
1,023 additions
and
33 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
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
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
Oops, something went wrong.