mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-20 00:28:52 +01:00
Select single words in property value
This commit is contained in:
committed by
Dirk Baeumer
parent
71aeb83f8a
commit
5be2260d22
@@ -4,7 +4,7 @@
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { getNode, getDeepestNode } from './util';
|
||||
import { getNode, getDeepestNode, findNextWord, findPrevWord } from './util';
|
||||
import Node from '@emmetio/node';
|
||||
|
||||
export function nextItemStylesheet(selection: vscode.Selection, editor: vscode.TextEditor, rootNode: Node): vscode.Selection {
|
||||
@@ -12,9 +12,17 @@ export function nextItemStylesheet(selection: vscode.Selection, editor: vscode.T
|
||||
let endOffset = editor.document.offsetAt(selection.active);
|
||||
let currentNode = getNode(rootNode, endOffset, true);
|
||||
|
||||
// Full property is selected, so select property value next
|
||||
// Full property is selected, so select full property value next
|
||||
if (currentNode.type === 'property' && startOffset === currentNode.start && endOffset === currentNode.end) {
|
||||
return getSelectionFromNode(currentNode, editor.document, true);
|
||||
return getSelectionFromProperty(currentNode, editor.document, startOffset, endOffset, true, 'next');
|
||||
}
|
||||
|
||||
// Part or whole of propertyValue is selected, so select the next word in the propertyValue
|
||||
if (currentNode.type === 'property' && startOffset >= currentNode.valueToken.start && endOffset <= currentNode.valueToken.end) {
|
||||
let singlePropertyValue = getSelectionFromProperty(currentNode, editor.document, startOffset, endOffset, false, 'next');
|
||||
if (singlePropertyValue) {
|
||||
return singlePropertyValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Cursor is in the selector or in a property
|
||||
@@ -41,13 +49,27 @@ export function nextItemStylesheet(selection: vscode.Selection, editor: vscode.T
|
||||
|
||||
export function prevItemStylesheet(selection: vscode.Selection, editor: vscode.TextEditor, rootNode: Node): vscode.Selection {
|
||||
let startOffset = editor.document.offsetAt(selection.anchor);
|
||||
let endOffset = editor.document.offsetAt(selection.active);
|
||||
let currentNode = getNode(rootNode, startOffset);
|
||||
if (!currentNode) {
|
||||
currentNode = rootNode;
|
||||
}
|
||||
|
||||
// Full property value is selected, so select the whole property next
|
||||
if (currentNode.type === 'property' && startOffset === currentNode.valueToken.start && endOffset === currentNode.valueToken.end) {
|
||||
return getSelectionFromNode(currentNode, editor.document);
|
||||
}
|
||||
|
||||
// Part of propertyValue is selected, so select the prev word in the propertyValue
|
||||
if (currentNode.type === 'property' && startOffset >= currentNode.valueToken.start && endOffset <= currentNode.valueToken.end) {
|
||||
let singlePropertyValue = getSelectionFromProperty(currentNode, editor.document, startOffset, endOffset, false, 'prev');
|
||||
if (singlePropertyValue) {
|
||||
return singlePropertyValue;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentNode.type === 'property' || !currentNode.firstChild || (currentNode.type === 'rule' && startOffset <= currentNode.firstChild.start)) {
|
||||
return getSelectionFromNode(currentNode, editor.document);;
|
||||
return getSelectionFromNode(currentNode, editor.document);
|
||||
}
|
||||
|
||||
// Select the child that appears just before the cursor
|
||||
@@ -57,23 +79,58 @@ export function prevItemStylesheet(selection: vscode.Selection, editor: vscode.T
|
||||
}
|
||||
prevNode = getDeepestNode(prevNode);
|
||||
|
||||
return getSelectionFromNode(prevNode, editor.document, true);
|
||||
return getSelectionFromProperty(prevNode, editor.document, startOffset, endOffset, false, 'prev');
|
||||
|
||||
}
|
||||
|
||||
|
||||
function getSelectionFromNode(node: Node, document: vscode.TextDocument, selectPropertyValue: boolean = false): vscode.Selection {
|
||||
function getSelectionFromNode(node: Node, document: vscode.TextDocument): vscode.Selection {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
let nodeToSelect = node.type === 'rule' ? node.selectorToken : node;
|
||||
return new vscode.Selection(document.positionAt(nodeToSelect.start), document.positionAt(nodeToSelect.end));
|
||||
}
|
||||
|
||||
let selectionStart = (node.type === 'property' && selectPropertyValue) ? node.valueToken.start : nodeToSelect.start;
|
||||
let selectionEnd = (node.type === 'property' && selectPropertyValue) ? node.valueToken.end : nodeToSelect.end;
|
||||
|
||||
return new vscode.Selection(document.positionAt(selectionStart), document.positionAt(selectionEnd));
|
||||
function getSelectionFromProperty(node: Node, document: vscode.TextDocument, selectionStart: number, selectionEnd: number, selectFullValue: boolean, direction: string): vscode.Selection {
|
||||
if (!node || node.type !== 'property') {
|
||||
return;
|
||||
}
|
||||
|
||||
let propertyValue = node.valueToken.stream.substring(node.valueToken.start, node.valueToken.end);
|
||||
selectFullValue = selectFullValue || (direction === 'prev' && selectionStart === node.valueToken.start && selectionEnd < node.valueToken.end);
|
||||
|
||||
if (selectFullValue) {
|
||||
return new vscode.Selection(document.positionAt(node.valueToken.start), document.positionAt(node.valueToken.end));
|
||||
}
|
||||
|
||||
let pos;
|
||||
if (direction === 'prev') {
|
||||
if (selectionStart === node.valueToken.start) {
|
||||
return;
|
||||
}
|
||||
pos = selectionStart > node.valueToken.end ? propertyValue.length : selectionStart - node.valueToken.start;
|
||||
}
|
||||
|
||||
if (direction === 'next') {
|
||||
if (selectionEnd === node.valueToken.end && (selectionStart > node.valueToken.start || propertyValue.indexOf(' ') === -1)) {
|
||||
return;
|
||||
}
|
||||
pos = selectionEnd === node.valueToken.end ? -1 : selectionEnd - node.valueToken.start - 1;
|
||||
}
|
||||
|
||||
|
||||
let [newSelectionStart, newSelectionEnd] = direction === 'prev' ? findPrevWord(propertyValue, pos) : findNextWord(propertyValue, pos);
|
||||
if (!newSelectionStart && !newSelectionEnd) {
|
||||
return;
|
||||
}
|
||||
|
||||
newSelectionStart += node.valueToken.start;
|
||||
newSelectionEnd += node.valueToken.start;
|
||||
|
||||
return new vscode.Selection(document.positionAt(newSelectionStart), document.positionAt(newSelectionEnd));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user