Skip to content

Commit bed41c9

Browse files
AndreasArvidssonpokey
authored andcommitted
Added modifier line number (#165)
* Added modifier line number * Added event that checks for edits outside of viewport * Improved warning message * Support ranges in line modifier
1 parent d20482c commit bed41c9

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/Types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ export interface SubpieceModifier {
9898
export interface MatchingPairSymbolModifier {
9999
type: "matchingPairSymbol";
100100
}
101+
export interface LineNumberModifierPosition {
102+
lineNumber: number;
103+
isRelative: boolean;
104+
}
105+
export interface LineNumberModifier {
106+
type: "lineNumber";
107+
anchor: LineNumberModifierPosition;
108+
active: LineNumberModifierPosition;
109+
}
101110
export interface IdentityModifier {
102111
type: "identity";
103112
}
@@ -109,11 +118,12 @@ export interface TailModifier {
109118
}
110119

111120
export type Modifier =
121+
| IdentityModifier
112122
| SurroundingPairModifier
113123
| ContainingScopeModifier
114124
| SubpieceModifier
115125
| MatchingPairSymbolModifier
116-
| IdentityModifier
126+
| LineNumberModifier
117127
| HeadModifier
118128
| TailModifier;
119129

src/extension.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,38 @@ export async function activate(context: vscode.ExtensionContext) {
215215

216216
addDecorationsDebounced();
217217

218+
function checkForEditsOutsideViewport(event: vscode.TextDocumentChangeEvent) {
219+
const editor = vscode.window.activeTextEditor;
220+
if (editor == null || editor.document !== event.document) {
221+
return;
222+
}
223+
const { start, end } = editor.visibleRanges[0];
224+
const ranges = [];
225+
for (const edit of event.contentChanges) {
226+
if (
227+
edit.range.end.isBeforeOrEqual(start) ||
228+
edit.range.start.isAfterOrEqual(end)
229+
) {
230+
ranges.push(edit.range);
231+
}
232+
}
233+
if (ranges.length > 0) {
234+
ranges.sort((a, b) => a.start.line - b.start.line);
235+
const linesText = ranges
236+
.map((range) => `${range.start.line + 1}-${range.end.line + 1}`)
237+
.join(", ");
238+
vscode.window.showWarningMessage(
239+
`Modification outside of viewport at lines: ${linesText}`
240+
);
241+
}
242+
}
243+
218244
function handleEdit(edit: vscode.TextDocumentChangeEvent) {
219245
graph.navigationMap.updateTokenRanges(edit);
220246

221247
addDecorationsDebounced();
248+
249+
checkForEditsOutsideViewport(edit);
222250
}
223251

224252
const recomputeDecorationStyles = async () => {

src/processTargets.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
Target,
1515
TypedSelection,
1616
Modifier,
17+
LineNumberModifierPosition,
1718
} from "./Types";
1819
import { performInsideOutsideAdjustment } from "./performInsideOutsideAdjustment";
1920
import { SUBWORD_MATCHER } from "./constants";
@@ -370,6 +371,27 @@ function transformSelection(
370371
];
371372
}
372373

374+
case "lineNumber": {
375+
const getLine = (linePosition: LineNumberModifierPosition) =>
376+
linePosition.isRelative
377+
? selection.editor.selection.active.line + linePosition.lineNumber
378+
: linePosition.lineNumber;
379+
return [
380+
{
381+
selection: update(selection, {
382+
selection: () =>
383+
new Selection(
384+
getLine(modifier.anchor),
385+
0,
386+
getLine(modifier.active),
387+
0
388+
),
389+
}),
390+
context: {},
391+
},
392+
];
393+
}
394+
373395
case "matchingPairSymbol":
374396
case "surroundingPair":
375397
throw new Error("Not implemented");

0 commit comments

Comments
 (0)