Skip to content

Commit

Permalink
Implementation of modulo line numbers (#333)
Browse files Browse the repository at this point in the history
* Implementation of modulo line numbers

* Cleaned up line numbers

* replaced CRLF line endings

* Handled folded regions

* Handled folded regions

* Properly handle folded regions

* Normalise recorded test cases

Co-authored-by: Pokey Rule <pokey.rule@gmail.com>
  • Loading branch information
AndreasArvidsson and pokey authored Nov 27, 2021
1 parent 1aa6ce3 commit 514c72d
Show file tree
Hide file tree
Showing 45 changed files with 428 additions and 93 deletions.
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ export async function activate(context: vscode.ExtensionContext) {
if (editor == null || editor.document !== event.document) {
return;
}
const { start, end } = editor.visibleRanges[0];
const { start } = editor.visibleRanges[0];
const { end } = editor.visibleRanges[editor.visibleRanges.length - 1];
const ranges = [];
for (const edit of event.contentChanges) {
if (
Expand Down
46 changes: 42 additions & 4 deletions src/processTargets/processMark.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { result } from "lodash";
import { Range, Selection } from "vscode";
import {
DecoratedSymbol,
Expand Down Expand Up @@ -151,10 +152,47 @@ function processDecoratedSymbol(
}

function processLineNumber(context: ProcessedTargetsContext, mark: LineNumber) {
const getLine = (linePosition: LineNumberPosition) =>
linePosition.isRelative
? context.currentEditor!.selection.active.line + linePosition.lineNumber
: linePosition.lineNumber;
const editor = context.currentEditor!;
const getLine = (linePosition: LineNumberPosition) => {
switch (linePosition.type) {
case "absolute":
return linePosition.lineNumber;
case "relative":
return editor.selection.active.line + linePosition.lineNumber;
case "modulo100":
const stepSize = 100;
const startLine = editor.visibleRanges[0].start.line;
const endLine =
editor.visibleRanges[editor.visibleRanges.length - 1].end.line;
const base = Math.floor(startLine / stepSize) * stepSize;
const visibleLines = [];
const invisibleLines = [];
let lineNumber = base + linePosition.lineNumber;
while (lineNumber <= endLine) {
if (lineNumber >= startLine) {
const visible = editor.visibleRanges.find(
(r) => lineNumber >= r.start.line && lineNumber <= r.end.line
);
if (visible) {
visibleLines.push(lineNumber);
} else {
invisibleLines.push(lineNumber);
}
}
lineNumber += stepSize;
}
if (visibleLines.length === 1) {
return visibleLines[0];
}
if (visibleLines.length + invisibleLines.length > 1) {
throw new Error("Multiple lines matching");
}
if (invisibleLines.length === 1) {
return invisibleLines[0];
}
throw new Error("Line is not in viewport");
}
};
return [
{
selection: new Selection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ command:
mark: {type: decoratedSymbol, symbolColor: default, character: h}
extraArgs: []
initialState:
documentContents: " hello world\r\n value = {a:2}"
documentContents: |2-
hello world
value = {a:2}
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Expand All @@ -23,7 +25,9 @@ initialState:
start: {line: 0, character: 4}
end: {line: 0, character: 9}
finalState:
documentContents: " {a:2}\r\n value = {a:2}"
documentContents: |2-
{a:2}
value = {a:2}
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ command:
mark: {type: decoratedSymbol, symbolColor: default, character: h}
extraArgs: []
initialState:
documentContents: " hello\r\n value = {a:2}"
documentContents: |2-
hello
value = {a:2}
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Expand All @@ -23,7 +25,9 @@ initialState:
start: {line: 0, character: 4}
end: {line: 0, character: 9}
finalState:
documentContents: " {a:2}\r\n value = {a:2}"
documentContents: |2-
{a:2}
value = {a:2}
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ command:
mark: {type: decoratedSymbol, symbolColor: default, character: a}
extraArgs: []
initialState:
documentContents: " hello\r\n value = {a:2}"
documentContents: |2-
hello
value = {a:2}
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Expand All @@ -23,7 +25,9 @@ initialState:
start: {line: 1, character: 13}
end: {line: 1, character: 14}
finalState:
documentContents: " hello\r\n value = hello"
documentContents: |2-
hello
value = hello
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Expand Down
10 changes: 8 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeAttribute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ command:
modifier: {type: containingScope, scopeType: attribute}
extraArgs: []
initialState:
documentContents: "[[nodiscard]]\r\nint f(int a = 1) {\r\n}\r\n"
documentContents: |
[[nodiscard]]
int f(int a = 1) {
}
selections:
- anchor: {line: 0, character: 9}
active: {line: 0, character: 9}
marks: {}
finalState:
documentContents: "[[nodiscard]]\r\nint f(int a = 1) {\r\n}\r\n"
documentContents: |
[[nodiscard]]
int f(int a = 1) {
}
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 13}
Expand Down
10 changes: 8 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeFunkName.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ command:
modifier: {type: containingScope, scopeType: functionName}
extraArgs: []
initialState:
documentContents: "int f(int a, int b) {\r\n \r\n}"
documentContents: |-
int f(int a, int b) {
}
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
marks: {}
finalState:
documentContents: "int f(int a, int b) {\r\n \r\n}"
documentContents: |-
int f(int a, int b) {
}
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 5}
Expand Down
10 changes: 8 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeFunkName2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ command:
modifier: {type: containingScope, scopeType: functionName}
extraArgs: []
initialState:
documentContents: "int C::f(int a, int b) {\r\n \r\n}"
documentContents: |-
int C::f(int a, int b) {
}
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
marks: {}
finalState:
documentContents: "int C::f(int a, int b) {\r\n \r\n}"
documentContents: |-
int C::f(int a, int b) {
}
selections:
- anchor: {line: 0, character: 7}
active: {line: 0, character: 8}
Expand Down
14 changes: 12 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeIf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ command:
modifier: {type: containingScope, scopeType: ifStatement}
extraArgs: []
initialState:
documentContents: "void f() {\r\n if (true) {\r\n \r\n }\r\n}"
documentContents: |-
void f() {
if (true) {
}
}
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 8}
marks: {}
finalState:
documentContents: "void f() {\r\n if (true) {\r\n \r\n }\r\n}"
documentContents: |-
void f() {
if (true) {
}
}
selections:
- anchor: {line: 1, character: 4}
active: {line: 3, character: 5}
Expand Down
14 changes: 12 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeIf2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ command:
modifier: {type: containingScope, scopeType: ifStatement}
extraArgs: []
initialState:
documentContents: "void f() {\r\n if constexpr (true) {\r\n \r\n }\r\n}"
documentContents: |-
void f() {
if constexpr (true) {
}
}
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 8}
marks: {}
finalState:
documentContents: "void f() {\r\n if constexpr (true) {\r\n \r\n }\r\n}"
documentContents: |-
void f() {
if constexpr (true) {
}
}
selections:
- anchor: {line: 1, character: 4}
active: {line: 3, character: 5}
Expand Down
16 changes: 14 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeItem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ command:
modifier: {type: containingScope, scopeType: collectionItem}
extraArgs: []
initialState:
documentContents: "void f() {\r\n std::vector<int> v {\r\n 1 + 2,\r\n 3\r\n };\r\n}"
documentContents: |-
void f() {
std::vector<int> v {
1 + 2,
3
};
}
selections:
- anchor: {line: 2, character: 10}
active: {line: 2, character: 10}
marks: {}
finalState:
documentContents: "void f() {\r\n std::vector<int> v {\r\n 1 + 2,\r\n 3\r\n };\r\n}"
documentContents: |-
void f() {
std::vector<int> v {
1 + 2,
3
};
}
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 13}
Expand Down
16 changes: 14 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeItem2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ command:
modifier: {type: containingScope, scopeType: collectionItem}
extraArgs: []
initialState:
documentContents: "void f() {\r\n int arr[] = {\r\n 1 + 2,\r\n 3\r\n };\r\n}"
documentContents: |-
void f() {
int arr[] = {
1 + 2,
3
};
}
selections:
- anchor: {line: 2, character: 11}
active: {line: 2, character: 11}
marks: {}
finalState:
documentContents: "void f() {\r\n int arr[] = {\r\n 1 + 2,\r\n 3\r\n };\r\n}"
documentContents: |-
void f() {
int arr[] = {
1 + 2,
3
};
}
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 13}
Expand Down
10 changes: 8 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeLambda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ command:
modifier: {type: containingScope, scopeType: anonymousFunction}
extraArgs: []
initialState:
documentContents: "void f() {\r\n [](){}();\r\n}\r\n"
documentContents: |
void f() {
[](){}();
}
selections:
- anchor: {line: 1, character: 7}
active: {line: 1, character: 7}
marks: {}
finalState:
documentContents: "void f() {\r\n [](){}();\r\n}\r\n"
documentContents: |
void f() {
[](){}();
}
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 10}
Expand Down
16 changes: 14 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeList.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ command:
modifier: {type: containingScope, scopeType: list}
extraArgs: []
initialState:
documentContents: "void f() {\r\n std::vector<int> v {\r\n 1 + 2,\r\n 3\r\n };\r\n}"
documentContents: |-
void f() {
std::vector<int> v {
1 + 2,
3
};
}
selections:
- anchor: {line: 2, character: 11}
active: {line: 2, character: 11}
marks: {}
finalState:
documentContents: "void f() {\r\n std::vector<int> v {\r\n 1 + 2,\r\n 3\r\n };\r\n}"
documentContents: |-
void f() {
std::vector<int> v {
1 + 2,
3
};
}
selections:
- anchor: {line: 1, character: 23}
active: {line: 4, character: 5}
Expand Down
16 changes: 14 additions & 2 deletions src/test/suite/fixtures/recorded/languages/cpp/takeList2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ command:
modifier: {type: containingScope, scopeType: list}
extraArgs: []
initialState:
documentContents: "void f() {\r\n int arr[] = {\r\n 1 + 2,\r\n 3\r\n };\r\n}"
documentContents: |-
void f() {
int arr[] = {
1 + 2,
3
};
}
selections:
- anchor: {line: 2, character: 12}
active: {line: 2, character: 12}
marks: {}
finalState:
documentContents: "void f() {\r\n int arr[] = {\r\n 1 + 2,\r\n 3\r\n };\r\n}"
documentContents: |-
void f() {
int arr[] = {
1 + 2,
3
};
}
selections:
- anchor: {line: 1, character: 16}
active: {line: 4, character: 5}
Expand Down
Loading

0 comments on commit 514c72d

Please sign in to comment.