Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/emmet/src/editPoint.ts
5240 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as vscode from 'vscode';
7
import { validate } from './util';
8
9
export function fetchEditPoint(direction: string): void {
10
if (!validate() || !vscode.window.activeTextEditor) {
11
return;
12
}
13
const editor = vscode.window.activeTextEditor;
14
15
const newSelections: vscode.Selection[] = [];
16
editor.selections.forEach(selection => {
17
const updatedSelection = direction === 'next' ? nextEditPoint(selection, editor) : prevEditPoint(selection, editor);
18
newSelections.push(updatedSelection);
19
});
20
editor.selections = newSelections;
21
editor.revealRange(editor.selections[editor.selections.length - 1]);
22
}
23
24
function nextEditPoint(selection: vscode.Selection, editor: vscode.TextEditor): vscode.Selection {
25
for (let lineNum = selection.anchor.line; lineNum < editor.document.lineCount; lineNum++) {
26
const updatedSelection = findEditPoint(lineNum, editor, selection.anchor, 'next');
27
if (updatedSelection) {
28
return updatedSelection;
29
}
30
}
31
return selection;
32
}
33
34
function prevEditPoint(selection: vscode.Selection, editor: vscode.TextEditor): vscode.Selection {
35
for (let lineNum = selection.anchor.line; lineNum >= 0; lineNum--) {
36
const updatedSelection = findEditPoint(lineNum, editor, selection.anchor, 'prev');
37
if (updatedSelection) {
38
return updatedSelection;
39
}
40
}
41
return selection;
42
}
43
44
45
function findEditPoint(lineNum: number, editor: vscode.TextEditor, position: vscode.Position, direction: string): vscode.Selection | undefined {
46
const line = editor.document.lineAt(lineNum);
47
let lineContent = line.text;
48
49
if (lineNum !== position.line && line.isEmptyOrWhitespace && lineContent.length) {
50
return new vscode.Selection(lineNum, lineContent.length, lineNum, lineContent.length);
51
}
52
53
if (lineNum === position.line && direction === 'prev') {
54
lineContent = lineContent.substr(0, position.character);
55
}
56
const emptyAttrIndex = direction === 'next' ? lineContent.indexOf('""', lineNum === position.line ? position.character : 0) : lineContent.lastIndexOf('""');
57
const emptyTagIndex = direction === 'next' ? lineContent.indexOf('><', lineNum === position.line ? position.character : 0) : lineContent.lastIndexOf('><');
58
59
let winner = -1;
60
61
if (emptyAttrIndex > -1 && emptyTagIndex > -1) {
62
winner = direction === 'next' ? Math.min(emptyAttrIndex, emptyTagIndex) : Math.max(emptyAttrIndex, emptyTagIndex);
63
} else if (emptyAttrIndex > -1) {
64
winner = emptyAttrIndex;
65
} else {
66
winner = emptyTagIndex;
67
}
68
69
if (winner > -1) {
70
return new vscode.Selection(lineNum, winner + 1, lineNum, winner + 1);
71
}
72
return;
73
}
74
75