[html] symbol highlighting tests

This commit is contained in:
Martin Aeschlimann 2016-09-13 22:12:48 +02:00
parent 0aefa275cb
commit 99ebc3065a
5 changed files with 60 additions and 33 deletions

View file

@ -24,6 +24,7 @@
"mimetypes": ["text/html", "text/x-jshtm", "text/template", "text/ng-template", "application/xhtml+xml"]
}],
"grammars": [{
"language": "html",
"scopeName": "text.html.basic",
"path": "./syntaxes/html.json"
}],

View file

@ -35,9 +35,8 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
capabilities: {
// Tell the client that the server works in FULL text document sync mode
textDocumentSync: documents.syncKind,
completionProvider: { resolveProvider: false, triggerCharacters: ['"', ':'] },
// hoverProvider: true,
// documentSymbolProvider: true,
completionProvider: { resolveProvider: false, triggerCharacters: ['.', ':', '<', '"', '=', '/'] },
documentHighlightProvider: true,
documentRangeFormattingProvider: true,
documentFormattingProvider: true
}
@ -122,21 +121,11 @@ connection.onCompletion(textDocumentPosition => {
return languageService.doComplete(document, textDocumentPosition.position, htmlDocument);
});
// connection.onCompletionResolve(completionItem => {
// return languageService.doResolve(completionItem);
// });
// connection.onHover(textDocumentPositionParams => {
// let document = documents.get(textDocumentPositionParams.textDocument.uri);
// let htmlDocument = getHTMLDocument(document);
// return languageService.doHover(document, textDocumentPositionParams.position, htmlDocument);
// });
// connection.onDocumentSymbol(documentSymbolParams => {
// let document = documents.get(documentSymbolParams.textDocument.uri);
// let htmlDocument = getHTMLDocument(document);
// return languageService.findDocumentSymbols(document, htmlDocument);
// });
connection.onDocumentHighlight(documentHighlightParams => {
let document = documents.get(documentHighlightParams.textDocument.uri);
let htmlDocument = getHTMLDocument(document);
return languageService.findDocumentHighlights(document, documentHighlightParams.position, htmlDocument);
});
function merge(src: any, dst: any) : any {
for (var key in src) {

View file

@ -41,7 +41,7 @@ export class Node {
if (idx >= 0) {
let child = this.children[idx];
if (offset > child.start && offset <= child.end) {
return child.findNodeBefore(offset);
return child.findNodeAt(offset);
}
}
return this;

View file

@ -11,15 +11,22 @@ import {TextDocument, Range, Position, DocumentHighlightKind, DocumentHighlight}
export function findDocumentHighlights(document: TextDocument, position: Position, htmlDocument: HTMLDocument): DocumentHighlight[] {
let offset = document.offsetAt(position);
let node = htmlDocument.findNodeAt(offset);
if (!node.tag || typeof node.endTagStart !== 'number') {
if (!node.tag) {
return [];
}
let result = [];
let startTagRange = getTagNameRange(TokenType.StartTag, document, node.start);
let endTagRange = getTagNameRange(TokenType.EndTag, document, node.endTagStart);
if (startTagRange && endTagRange && (covers(startTagRange, position) || covers(endTagRange, position))) {
return [ { kind: DocumentHighlightKind.Read, range: startTagRange }, { kind: DocumentHighlightKind.Read, range: endTagRange }];
let endTagRange = typeof node.endTagStart === 'number' && getTagNameRange(TokenType.EndTag, document, node.endTagStart);
if (startTagRange && covers(startTagRange, position) || endTagRange && covers(endTagRange, position)) {
if (startTagRange) {
result.push({ kind: DocumentHighlightKind.Read, range: startTagRange });
}
if (endTagRange) {
result.push({ kind: DocumentHighlightKind.Read, range: endTagRange });
}
}
return [];
console.log('foo' + result.length);
return result;
}
function isBeforeOrEqual(pos1: Position, pos2: Position) {
@ -30,10 +37,10 @@ function covers(range: Range, position: Position) {
return isBeforeOrEqual(range.start, position) && isBeforeOrEqual(position, range.end);
}
function getTagNameRange(tokenType: TokenType, document: TextDocument, startOffset: number ) : Range {
function getTagNameRange(tokenType: TokenType, document: TextDocument, startOffset: number) : Range {
let scanner = createScanner(document.getText(), startOffset);
let token = scanner.scan();
while (token !== TokenType.EOS && token !== TokenType.StartTag) {
while (token !== TokenType.EOS && token !== tokenType) {
token = scanner.scan();
}
if (token !== TokenType.EOS) {

View file

@ -8,7 +8,7 @@ import * as assert from 'assert';
import * as htmlLanguageService from '../htmlLanguageService';
import {CompletionList, TextDocument, TextEdit, Position, CompletionItemKind} from 'vscode-languageserver-types';
export function assertHighlights(value: string, expectedMatches: number[], elementName: string): Thenable<void> {
export function assertHighlights(value: string, expectedMatches: number[], elementName: string): void {
let offset = value.indexOf('|');
value = value.substr(0, offset) + value.substr(offset + 1);
@ -33,11 +33,41 @@ export function assertHighlights(value: string, expectedMatches: number[], eleme
suite('HTML Highlighting', () => {
test('Single', function (): any {
assertHighlights('|<html></html>', [], null);
assertHighlights('<|html></html>', [1, 8], 'html');
assertHighlights('<h|tml></html>', [1, 8], 'html');
assertHighlights('<htm|l></html>', [1, 8], 'html');
assertHighlights('<html|></html>', [1, 8], 'html');
assertHighlights('<html>|</html>', [], null);
assertHighlights('<html><|/html>', [], null);
assertHighlights('<html></|html>', [1, 8], 'html');
assertHighlights('<html></h|tml>', [1, 8], 'html');
assertHighlights('<html></ht|ml>', [1, 8], 'html');
assertHighlights('<html></htm|l>', [1, 8], 'html');
assertHighlights('<html></html|>', [1, 8], 'html');
assertHighlights('<html></html>|', [], null);
});
test('Nested', function (): any {
assertHighlights('<html>|<div></div></html>', [], null);
assertHighlights('<html><|div></div></html>', [7, 13], 'div');
assertHighlights('<html><div>|</div></html>', [], null);
assertHighlights('<html><div></di|v></html>', [7, 13], 'div');
assertHighlights('<html><div><div></div></di|v></html>', [7, 24], 'div');
assertHighlights('<html><div><div></div|></div></html>', [12, 18], 'div');
assertHighlights('<html><div><div|></div></div></html>', [12, 18], 'div');
assertHighlights('<html><div><div></div></div></h|tml>', [1, 30], 'html');
assertHighlights('<html><di|v></div><div></div></html>', [7, 13], 'div');
assertHighlights('<html><div></div><div></d|iv></html>', [18, 24], 'div');
});
test('Selfclosed', function (): any {
assertHighlights('<html><|div/></html>', [ 7 ], 'div');
assertHighlights('<html><|br></html>', [ 7 ], 'br');
assertHighlights('<html><div><d|iv/></div></html>', [ 12 ], 'div');
});
test('Highlighting', function (testDone): any {
testHighlighting
}
});