-
Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathgotoLineQuickAccess.ts
179 lines (141 loc) · 6.34 KB
/
gotoLineQuickAccess.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { Disposable, DisposableStore, IDisposable, toDisposable } from '../../../../base/common/lifecycle.js';
import { getCodeEditor } from '../../../browser/editorBrowser.js';
import { EditorOption, RenderLineNumbersType } from '../../../common/config/editorOptions.js';
import { IPosition } from '../../../common/core/position.js';
import { IRange } from '../../../common/core/range.js';
import { IEditor, ScrollType } from '../../../common/editorCommon.js';
import { AbstractEditorNavigationQuickAccessProvider, IQuickAccessTextEditorContext } from './editorNavigationQuickAccess.js';
import { localize } from '../../../../nls.js';
import { IQuickPick, IQuickPickItem } from '../../../../platform/quickinput/common/quickInput.js';
interface IGotoLineQuickPickItem extends IQuickPickItem, Partial<IPosition> { }
export abstract class AbstractGotoLineQuickAccessProvider extends AbstractEditorNavigationQuickAccessProvider {
static PREFIX = ':';
constructor() {
super({ canAcceptInBackground: true });
}
protected provideWithoutTextEditor(picker: IQuickPick<IGotoLineQuickPickItem, { useSeparators: true }>): IDisposable {
const label = localize('cannotRunGotoLine', "Open a text editor first to go to a line.");
picker.items = [{ label }];
picker.ariaLabel = label;
return Disposable.None;
}
protected provideWithTextEditor(context: IQuickAccessTextEditorContext, picker: IQuickPick<IGotoLineQuickPickItem, { useSeparators: true }>, token: CancellationToken): IDisposable {
const editor = context.editor;
const disposables = new DisposableStore();
// Goto line once picked
disposables.add(picker.onDidAccept(event => {
const [item] = picker.selectedItems;
if (item) {
if (!this.isValidLineNumber(editor, item.lineNumber)) {
return;
}
this.gotoLocation(context, { range: this.toRange(item.lineNumber, item.column), keyMods: picker.keyMods, preserveFocus: event.inBackground });
if (!event.inBackground) {
picker.hide();
}
}
}));
// React to picker changes
const updatePickerAndEditor = () => {
const position = this.parsePosition(editor, picker.value.trim().substr(AbstractGotoLineQuickAccessProvider.PREFIX.length));
const label = this.getPickLabel(editor, position.lineNumber, position.column);
// Picker
picker.items = [{
lineNumber: position.lineNumber,
column: position.column,
label
}];
// ARIA Label
picker.ariaLabel = label;
// Clear decorations for invalid range
if (!this.isValidLineNumber(editor, position.lineNumber)) {
this.clearDecorations(editor);
return;
}
// Reveal
const range = this.toRange(position.lineNumber, position.column);
editor.revealRangeInCenter(range, ScrollType.Smooth);
// Decorate
this.addDecorations(editor, range);
};
updatePickerAndEditor();
disposables.add(picker.onDidChangeValue(() => updatePickerAndEditor()));
// Adjust line number visibility as needed
const codeEditor = getCodeEditor(editor);
if (codeEditor) {
const options = codeEditor.getOptions();
const lineNumbers = options.get(EditorOption.lineNumbers);
if (lineNumbers.renderType === RenderLineNumbersType.Relative) {
codeEditor.updateOptions({ lineNumbers: 'on' });
disposables.add(toDisposable(() => codeEditor.updateOptions({ lineNumbers: 'relative' })));
}
}
return disposables;
}
private toRange(lineNumber = 1, column = 1): IRange {
return {
startLineNumber: lineNumber,
startColumn: column,
endLineNumber: lineNumber,
endColumn: column
};
}
private parsePosition(editor: IEditor, value: string): IPosition {
// Support line-col formats of `line,col`, `line:col`, `line#col`
const parts = value.split(/,|:|#/).filter(part => !isNaN(parseInt(part, 10)));
const numbers = parts.map(part => parseInt(part, 10));
// If number starts with + or - we add it to the current cursor position for relative positioning
const currentPosition = editor.getPosition() || { lineNumber: 1, column: 1 };
const lineNumberRelative = parts.length > 0 && /^[+-]/.test(parts[0]);
const columnRelative = parts.length > 1 && /^[+-]/.test(parts[1]);
return {
lineNumber: lineNumberRelative
? (numbers[0] + currentPosition.lineNumber)
: numbers[0],
column: columnRelative
? (numbers[1] + currentPosition.column)
: numbers[1]
};
}
private getPickLabel(editor: IEditor, lineNumber: number, column: number | undefined): string {
// Location valid: indicate this as picker label
if (this.isValidLineNumber(editor, lineNumber)) {
if (this.isValidColumn(editor, lineNumber, column)) {
return localize('gotoLineColumnLabel', "Go to line {0} and character {1}.", lineNumber, column);
}
return localize('gotoLineLabel', "Go to line {0}.", lineNumber);
}
// Location invalid: show generic label
const position = editor.getPosition() || { lineNumber: 1, column: 1 };
const lineCount = this.lineCount(editor);
if (lineCount > 1) {
return localize('gotoLineLabelEmptyWithLimit', "Current Line: {0}, Character: {1}. Type a line number between 1 and {2} to navigate to.", position.lineNumber, position.column, lineCount);
}
return localize('gotoLineLabelEmpty', "Current Line: {0}, Character: {1}. Type a line number to navigate to.", position.lineNumber, position.column);
}
private isValidLineNumber(editor: IEditor, lineNumber: number | undefined): boolean {
if (!lineNumber || typeof lineNumber !== 'number') {
return false;
}
return lineNumber > 0 && lineNumber <= this.lineCount(editor);
}
private isValidColumn(editor: IEditor, lineNumber: number, column: number | undefined): boolean {
if (!column || typeof column !== 'number') {
return false;
}
const model = this.getModel(editor);
if (!model) {
return false;
}
const positionCandidate = { lineNumber, column };
return model.validatePosition(positionCandidate).equals(positionCandidate);
}
private lineCount(editor: IEditor): number {
return this.getModel(editor)?.getLineCount() ?? 0;
}
}