-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selection Range support. Part of Microsoft/vscode#65925 #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "0.1.0", | ||
"command": "npm", | ||
"isShellCommand": true, | ||
"showOutput": "always", | ||
"suppressTaskName": true, | ||
"tasks": [ | ||
{ | ||
"taskName": "compile", | ||
"args": ["run", "compile"] | ||
} | ||
] | ||
} | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "npm", | ||
"script": "compile", | ||
"problemMatcher": "$tsc-watch", | ||
"isBackground": true, | ||
"presentation": { | ||
"reveal": "never" | ||
}, | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
} | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/** | ||
* Until SelectionRange lands in LSP, we'll return Range from server and convert it to | ||
* SelectionRange on client side | ||
*/ | ||
import { Range, TextDocument, Position } from 'vscode-languageserver-types'; | ||
import { createScanner } from '../parser/htmlScanner'; | ||
import { parse, Node } from '../parser/htmlParser'; | ||
import { TokenType } from '../htmlLanguageTypes'; | ||
|
||
export function getSelectionRanges(document: TextDocument, position: Position) { | ||
const applicableRanges = getApplicableRanges(document, position); | ||
const ranges = applicableRanges.map(pair => { | ||
return Range.create( | ||
document.positionAt(pair[0]), | ||
document.positionAt(pair[1]) | ||
); | ||
}); | ||
return ranges; | ||
} | ||
|
||
export function getApplicableRanges(document: TextDocument, position: Position): number[][] { | ||
const htmlDoc = parse(document.getText()); | ||
const currOffset = document.offsetAt(position); | ||
const currNode = htmlDoc.findNodeAt(currOffset); | ||
|
||
let result = getAllParentTagRanges(currNode); | ||
|
||
if (!currNode.startTagEnd || !currNode.endTagStart) { | ||
return result; | ||
} | ||
|
||
/** | ||
* For html like | ||
* `<div class="foo">bar</div>` | ||
*/ | ||
|
||
/** | ||
* Cursor inside `<div class="foo">` | ||
*/ | ||
if (currNode.start < currOffset && currOffset < currNode.startTagEnd) { | ||
result.unshift([currNode.start + 1, currNode.startTagEnd - 1]); | ||
const attributeLevelRanges = getAttributeLevelRanges(document, currNode, currOffset); | ||
result = attributeLevelRanges.concat(result); | ||
return result; | ||
} | ||
/** | ||
* Cursor inside `bar` | ||
*/ | ||
else if (currNode.startTagEnd <= currOffset && currOffset <= currNode.endTagStart) { | ||
result.unshift([currNode.startTagEnd, currNode.endTagStart]); | ||
|
||
return result; | ||
} | ||
/** | ||
* Cursor inside `</div>` | ||
*/ | ||
else { | ||
// `div` inside `</div>` | ||
if (currOffset >= currNode.endTagStart + 2) { | ||
result.unshift([currNode.endTagStart + 2, currNode.end - 1]); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
function getAllParentTagRanges(initialNode: Node) { | ||
let currNode = initialNode; | ||
|
||
const getNodeRanges = (n: Node) => { | ||
if (n.startTagEnd && n.endTagStart && n.startTagEnd < n.endTagStart) { | ||
return [ | ||
[n.startTagEnd, n.endTagStart], | ||
[n.start, n.end] | ||
]; | ||
} | ||
|
||
return [ | ||
[n.start, n.end] | ||
]; | ||
}; | ||
|
||
const result: number[][] = []; | ||
|
||
while (currNode.parent) { | ||
currNode = currNode.parent; | ||
getNodeRanges(currNode).forEach(r => result.push(r)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function getAttributeLevelRanges(document: TextDocument, currNode: Node, currOffset: number) { | ||
const currNodeRange = Range.create(document.positionAt(currNode.start), document.positionAt(currNode.end)); | ||
const currNodeText = document.getText(currNodeRange); | ||
const relativeOffset = currOffset - currNode.start; | ||
|
||
/** | ||
* Tag level semantic selection | ||
*/ | ||
|
||
const scanner = createScanner(currNodeText); | ||
let token = scanner.scan(); | ||
|
||
/** | ||
* For text like | ||
* <div class="foo">bar</div> | ||
*/ | ||
const positionOffset = currNode.start; | ||
|
||
const result = []; | ||
|
||
let isInsideAttribute = false; | ||
let attrStart = -1; | ||
while (token !== TokenType.EOS) { | ||
switch (token) { | ||
case TokenType.AttributeName: { | ||
if (relativeOffset < scanner.getTokenOffset()) { | ||
isInsideAttribute = false; | ||
break; | ||
} | ||
|
||
if (relativeOffset <= scanner.getTokenEnd()) { | ||
// `class` | ||
result.unshift([scanner.getTokenOffset(), scanner.getTokenEnd()]); | ||
} | ||
|
||
isInsideAttribute = true; | ||
attrStart = scanner.getTokenOffset(); | ||
break; | ||
} | ||
case TokenType.AttributeValue: { | ||
if (!isInsideAttribute) { | ||
break; | ||
} | ||
|
||
const valueText = scanner.getTokenText(); | ||
if (relativeOffset < scanner.getTokenOffset()) { | ||
// `class="foo"` | ||
result.push([attrStart, scanner.getTokenEnd()]); | ||
break; | ||
} | ||
|
||
if (relativeOffset >= scanner.getTokenOffset() && relativeOffset <= scanner.getTokenEnd()) { | ||
// `"foo"` | ||
result.unshift([scanner.getTokenOffset(), scanner.getTokenEnd()]); | ||
|
||
// `foo` | ||
if ((valueText[0] === `"` && valueText[valueText.length - 1] === `"`) || (valueText[0] === `'` && valueText[valueText.length - 1] === `'`)) { | ||
if (relativeOffset >= scanner.getTokenOffset() + 1 && relativeOffset <= scanner.getTokenEnd() - 1) { | ||
result.unshift([scanner.getTokenOffset() + 1, scanner.getTokenEnd() - 1]); | ||
} | ||
} | ||
|
||
// `class="foo"` | ||
result.push([attrStart, scanner.getTokenEnd()]); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
token = scanner.scan(); | ||
} | ||
|
||
return result.map(pair => { | ||
return [pair[0] + positionOffset, pair[1] + positionOffset]; | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import 'mocha'; | ||
import * as assert from 'assert'; | ||
import { TextDocument } from 'vscode-languageserver-types'; | ||
import { getApplicableRanges } from '../services/htmlSelectionRange'; | ||
|
||
function assertRanges(lines: string[] | string, expected: number[][]): void { | ||
let content: string = ''; | ||
if (Array.isArray(lines)) { | ||
content = lines.join('\n'); | ||
} else { | ||
content = lines; | ||
} | ||
let message = `Test ${content}`; | ||
|
||
let offset = content.indexOf('|'); | ||
content = content.substr(0, offset) + content.substr(offset + 1); | ||
|
||
const document = TextDocument.create('test://foo/bar.html', 'html', 1, content); | ||
const actualRanges = getApplicableRanges(document, document.positionAt(offset)); | ||
|
||
message += `\n${JSON.stringify(actualRanges)} should equal to ${JSON.stringify(expected)}`; | ||
assert.deepEqual(actualRanges, expected, message); | ||
} | ||
|
||
suite('HTML SelectionRange', () => { | ||
test('Basic', () => { | ||
assertRanges('<div|>foo</div>', [[1, 4], [0, 14]]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't [1,4] be [1,3]? |
||
assertRanges('<|div>foo</div>', [[1, 4], [0, 14]]); | ||
assertRanges('<d|iv>foo</div>', [[1, 4], [0, 14]]); | ||
|
||
assertRanges('<div>|foo</div>', [[5, 8], [0, 14]]); | ||
assertRanges('<div>f|oo</div>', [[5, 8], [0, 14]]); | ||
assertRanges('<div>foo|</div>', [[5, 8], [0, 14]]); | ||
|
||
assertRanges('<div>foo<|/div>', [[0, 14]]); | ||
|
||
assertRanges('<div>foo</|div>', [[10, 13], [0, 14]]); | ||
assertRanges('<div>foo</di|v>', [[10, 13], [0, 14]]); | ||
assertRanges('<div>foo</div|>', [[10, 13], [0, 14]]); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also test self closed tags ( |
||
|
||
test('Attribute Name', () => { | ||
assertRanges('<div |class="foo">foo</div>', [[5, 10], [5, 16], [1, 16], [0, 26]]); | ||
assertRanges('<div cl|ass="foo">foo</div>', [[5, 10], [5, 16], [1, 16], [0, 26]]); | ||
assertRanges('<div class|="foo">foo</div>', [[5, 10], [5, 16], [1, 16], [0, 26]]); | ||
}); | ||
|
||
test('Attribute Value', () => { | ||
assertRanges('<div class=|"foo">foo</div>', [[11, 16], [5, 16], [1, 16], [0, 26]]); | ||
assertRanges('<div class="foo"|>foo</div>', [[11, 16], [5, 16], [1, 16], [0, 26]]); | ||
|
||
assertRanges('<div class="|foo">foo</div>', [[12, 15], [11, 16], [5, 16], [1, 16], [0, 26]]); | ||
assertRanges('<div class="f|oo">foo</div>', [[12, 15], [11, 16], [5, 16], [1, 16], [0, 26]]); | ||
}); | ||
|
||
test('Unquoted Attribute Value', () => { | ||
assertRanges('<div class=|foo>foo</div>', [[11, 14], [5, 14], [1, 14], [0, 24]]); | ||
}); | ||
|
||
test('Multiple Attribute Value', () => { | ||
assertRanges('<div class="foo" id="|bar">foo</div>', [[21, 24], [20, 25], [17, 25], [1, 25], [0, 35]]); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test comments and doctypes as well |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally write the tests against the public API (htmlLanguageService.getSelectionRanges), that way we are sure it works end-to-end and the implementation can be changed freely without impacting any tests.