Skip to content

Token removal range based upon merging of tokens or not #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Range } from "vscode";
import { Range, TextDocument } from "vscode";
import { tokenize } from "../../../core/tokenizer";
import type { Target } from "../../../typings/target.types";
import { isAtEndOfLine, isAtStartOfLine } from "../../../util/rangeUtils";
import { expandToFullLine } from "../../../util/rangeUtils";
import { PlainTarget } from "../../targets";
import { getDelimitedSequenceRemovalRange } from "./DelimitedSequenceInsertionRemovalBehavior";

Expand Down Expand Up @@ -66,24 +67,48 @@ export function getTokenRemovalRange(
target: Target,
contentRange?: Range
): Range {
const { start, end } = contentRange ?? target.contentRange;
const { document } = target.editor;
const actualContentRange = contentRange ?? target.contentRange;
const removalRange = getDelimitedSequenceRemovalRange(target, contentRange);

const leadingDelimiterTarget = getTokenLeadingDelimiterTarget(target);
const trailingDelimiterTarget = getTokenTrailingDelimiterTarget(target);
if (!actualContentRange.isEqual(removalRange)) {
const fullRange = expandToFullLine(target.editor, actualContentRange);
const fullText = document.getText(fullRange);
const fullTextOffset = document.offsetAt(fullRange.start);

// If there is a token directly to the left or right of us with no
// separating white space, then we might join two tokens if we try to clean
// up whitespace space. In this case we just remove the content range
// without attempting to clean up white space.
//
// In the future, we might get more sophisticated and to clean up white space if we can detect that it won't cause two tokens be merged
if (
(leadingDelimiterTarget == null && !isAtStartOfLine(start)) ||
(trailingDelimiterTarget == null && !isAtEndOfLine(target.editor, end))
) {
return contentRange ?? target.contentRange;
const numTokensContentRangeRemoved = calculateNumberOfTokensAfterRemoval(
document,
fullText,
fullTextOffset,
actualContentRange
);

const numTokensRemovalRangeRemoved = calculateNumberOfTokensAfterRemoval(
document,
fullText,
fullTextOffset,
removalRange
);

// Using removal range has not merged any tokens. Removal range is ok to use.
if (numTokensContentRangeRemoved === numTokensRemovalRangeRemoved) {
return removalRange;
}
}

// Otherwise, behave like a whitespace delimited sequence
return getDelimitedSequenceRemovalRange(target, contentRange);
// No removal range available or it would merge tokens.
return actualContentRange;
}

function calculateNumberOfTokensAfterRemoval(
document: TextDocument,
fullText: string,
fullTextOffset: number,
removalRange: Range
): number {
const startIndex = document.offsetAt(removalRange.start) - fullTextOffset;
const endIndex = document.offsetAt(removalRange.end) - fullTextOffset;
const modifiedText = fullText.slice(0, startIndex) + fullText.slice(endIndex);
const tokens = tokenize(modifiedText, document.languageId, (m) => m);
return tokens.length;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ initialState:
start: {line: 0, character: 23}
end: {line: 0, character: 29}
finalState:
documentContents: "function myFunk(value: ) { };"
documentContents: function myFunk(value:) { };
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
thatMark:
- type: UntypedTarget
contentRange:
start: {line: 0, character: 23}
end: {line: 0, character: 23}
start: {line: 0, character: 22}
end: {line: 0, character: 22}
isReversed: false
hasExplicitRange: false
fullTargets: [{type: list, elements: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: m}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: argumentOrParameter, includeSiblings: false}, isImplicit: false}, {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: a}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: argumentOrParameter, includeSiblings: false}, isImplicit: false}, {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: s}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: toRawSelection}, isImplicit: false}]}]
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ initialState:
active: {line: 0, character: 16}
marks: {}
finalState:
documentContents: "@namespace ;"
documentContents: "@namespace;"
selections:
- anchor: {line: 0, character: 11}
active: {line: 0, character: 11}
- anchor: {line: 0, character: 10}
active: {line: 0, character: 10}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: value, includeSiblings: false}, isImplicit: false}]
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ initialState:
active: {line: 0, character: 6}
marks: {}
finalState:
documentContents: "@namespace ;"
documentContents: "@namespace;"
selections:
- anchor: {line: 0, character: 6}
active: {line: 0, character: 6}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ initialState:
finalState:
documentContents: |
a {
margin-top: ;
margin-top:;
}
selections:
- anchor: {line: 1, character: 7}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ initialState:
end: {line: 0, character: 20}
finalState:
documentContents: |
let user = ;
let user =;
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ initialState:
end: {line: 0, character: 2}
finalState:
documentContents: |-
fn () {
fn() {
println!("Hello, world!");
}
selections:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ initialState:
start: {line: 0, character: 9}
end: {line: 0, character: 10}
finalState:
documentContents: let x = ;
documentContents: let x =;
selections:
- anchor: {line: 0, character: 9}
active: {line: 0, character: 9}
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: '1'}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: list, includeSiblings: false}, isImplicit: false}]
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ initialState:
active: {line: 0, character: 5}
marks: {}
finalState:
documentContents: "let mut : [i32; 3] = [0; 3];"
documentContents: "let mut: [i32; 3] = [0; 3];"
selections:
- anchor: {line: 0, character: 5}
active: {line: 0, character: 5}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ initialState:
marks: {}
finalState:
documentContents: |
fn () -> Result<(), Error> {
fn() -> Result<(), Error> {

}
selections:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ initialState:
active: {line: 0, character: 16}
marks: {}
finalState:
documentContents: "@namespace ;"
documentContents: "@namespace;"
selections:
- anchor: {line: 0, character: 11}
active: {line: 0, character: 11}
- anchor: {line: 0, character: 10}
active: {line: 0, character: 10}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: value, includeSiblings: false}, isImplicit: false}]
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ initialState:
active: {line: 0, character: 6}
marks: {}
finalState:
documentContents: "@namespace ;"
documentContents: "@namespace;"
selections:
- anchor: {line: 0, character: 6}
active: {line: 0, character: 6}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ initialState:
finalState:
documentContents: |
a {
margin-top: ;
margin-top:;
}
selections:
- anchor: {line: 1, character: 7}
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/fixtures/recorded/positions/chuckAir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ finalState:
"hello "
" hello"
" hello "
return .b
return.b
selections:
- anchor: {line: 4, character: 0}
active: {line: 4, character: 0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ initialState:
start: {line: 0, character: 1}
end: {line: 0, character: 4}
finalState:
documentContents: "[ bar]"
documentContents: "[bar]"
selections:
- anchor: {line: 0, character: 1}
active: {line: 0, character: 1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ initialState:
start: {line: 0, character: 4}
end: {line: 0, character: 8}
finalState:
documentContents: f.o( text)
documentContents: f.o(text)
selections:
- anchor: {line: 0, character: 10}
active: {line: 0, character: 10}
- anchor: {line: 0, character: 9}
active: {line: 0, character: 9}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: s}, modifiers: [{type: containingScope, scopeType: {type: boundedNonWhitespaceSequence}}]}]