Skip to content
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

Bugfixes: past token, past end of, subtoken out of range, sort tokens #229

Merged
merged 18 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
30 changes: 29 additions & 1 deletion src/NavigationMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,36 @@ export default class NavigationMap {
}

public getTokenForRange(range: Range) {
return Object.values(this.map).find(
const matches = Object.values(this.map).filter(
(token) => token.range.intersection(range) != null
);

// If multiple matches sort and take the first
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
matches.sort((a, b) => {
// First sort on alphanumeric
const aIsAlphaNum = isAlphaNum(a.text);
const bIsAlphaNum = isAlphaNum(b.text);
if (aIsAlphaNum && !bIsAlphaNum) {
return -1;
}
if (bIsAlphaNum && !aIsAlphaNum) {
return 1;
}

// Second sort on length
const lengthDiff = b.text.length - a.text.length;
if (lengthDiff !== 0) {
return lengthDiff;
}

// Lastly sort on start position. ie leftmost
return a.startOffset - b.startOffset;
});

return matches[0] ?? null;
}
}

function isAlphaNum(text: string) {
return /^\w+$/.test(text);
}
6 changes: 5 additions & 1 deletion src/inferFullTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,10 @@ function inferRangeStartTarget(
prototypeTargets: Target[],
actionPreferences: ActionPreferences
): PrimitiveTarget {
const mark = target.mark ?? CURSOR_MARK;
const mark =
target.mark ??
(target.selectionType === "token" ? CURSOR_MARK_TOKEN : CURSOR_MARK);

prototypeTargets = hasContent(target) ? [] : prototypeTargets;

const selectionType =
Expand Down Expand Up @@ -377,6 +380,7 @@ export function inferRangeEndTarget(

const mark =
target.mark ??
(target.selectionType === "token" ? CURSOR_MARK_TOKEN : null) ??
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
extractAttributeFromList(
possiblePrototypeTargetsIncludingStartTarget,
"mark"
Expand Down
71 changes: 46 additions & 25 deletions src/processTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
SelectionWithEditor,
Target,
TypedSelection,
Position as TargetPosition,
InsideOutsideType,
} from "./Types";

export default function processTargets(
Expand Down Expand Up @@ -334,6 +336,15 @@ function transformSelection(
const activeIndex =
modifier.active < 0 ? modifier.active + pieces.length : modifier.active;

if (
anchorIndex < 0 ||
activeIndex < 0 ||
anchorIndex >= pieces.length ||
activeIndex >= pieces.length
) {
throw new Error("Subtoken index out of range");
}

const isReversed = activeIndex < anchorIndex;

const anchor = selection.selection.start.translate(
Expand Down Expand Up @@ -461,6 +472,8 @@ function createTypedSelection(
selectionContext: getTokenSelectionContext(
selection,
modifier,
position,
insideOutsideType,
selectionContext
),
};
Expand Down Expand Up @@ -600,6 +613,8 @@ function performPositionAdjustment(
function getTokenSelectionContext(
selection: SelectionWithEditor,
modifier: Modifier,
position: TargetPosition,
insideOutsideType: InsideOutsideType,
selectionContext: SelectionContext
): SelectionContext {
if (!isSelectionContextEmpty(selectionContext)) {
Expand All @@ -611,32 +626,38 @@ function getTokenSelectionContext(

const document = selection.editor.document;
const { start, end } = selection.selection;

const startLine = document.lineAt(start);
const leadingText = startLine.text.slice(0, start.character);
const leadingDelimiters = leadingText.match(/\s+$/);
const leadingDelimiterRange =
leadingDelimiters != null
? new Range(
start.line,
start.character - leadingDelimiters[0].length,
start.line,
start.character
)
: null;

const endLine = document.lineAt(end);
const trailingText = endLine.text.slice(end.character);
const trailingDelimiters = trailingText.match(/^\s+/);
const trailingDelimiterRange =
trailingDelimiters != null
? new Range(
end.line,
end.character,
end.line,
end.character + trailingDelimiters[0].length
)
: null;
let leadingDelimiterRange, trailingDelimiterRange;

// Position start/end of has no delimiter
if (position !== "before" || insideOutsideType !== "inside") {
const startLine = document.lineAt(start);
const leadingText = startLine.text.slice(0, start.character);
const leadingDelimiters = leadingText.match(/\s+$/);
leadingDelimiterRange =
leadingDelimiters != null
? new Range(
start.line,
start.character - leadingDelimiters[0].length,
start.line,
start.character
)
: null;
}

if (position !== "after" || insideOutsideType !== "inside") {
const trailingText = endLine.text.slice(end.character);
const trailingDelimiters = trailingText.match(/^\s+/);
trailingDelimiterRange =
trailingDelimiters != null
? new Range(
end.line,
end.character,
end.line,
end.character + trailingDelimiters[0].length
)
: null;
}

const isInDelimitedList =
(leadingDelimiterRange != null || trailingDelimiterRange != null) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: take past end of token
languageId: typescript
command:
actionName: setSelection
partialTargets:
- type: range
start: {type: primitive}
end: {type: primitive, position: after, insideOutsideType: inside, selectionType: token}
excludeStart: false
excludeEnd: false
extraArgs: []
marks: {}
initialState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
finalState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 11}
thatMark:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 11}
fullTargets: [{type: range, excludeStart: false, excludeEnd: false, start: {type: primitive, mark: {type: cursor}, selectionType: token, position: contents, modifier: {type: identity}, insideOutsideType: inside}, end: {type: primitive, mark: {type: cursorToken}, selectionType: token, position: after, modifier: {type: identity}, insideOutsideType: inside}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: take past start of token
languageId: typescript
command:
actionName: setSelection
partialTargets:
- type: range
start: {type: primitive}
end: {type: primitive, position: before, insideOutsideType: inside, selectionType: token}
excludeStart: false
excludeEnd: false
extraArgs: []
marks: {}
initialState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
finalState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 6}
thatMark:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 6}
fullTargets: [{type: range, excludeStart: false, excludeEnd: false, start: {type: primitive, mark: {type: cursor}, selectionType: token, position: contents, modifier: {type: identity}, insideOutsideType: inside}, end: {type: primitive, mark: {type: cursorToken}, selectionType: token, position: before, modifier: {type: identity}, insideOutsideType: inside}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
spokenForm: take token past trap
languageId: typescript
command:
actionName: setSelection
partialTargets:
- type: range
start: {type: primitive, selectionType: token}
end:
type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: t}
excludeStart: false
excludeEnd: false
extraArgs: []
marks:
default.t:
start: {line: 0, character: 6}
end: {line: 0, character: 11}
initialState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 3}
active: {line: 0, character: 3}
finalState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 11}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 11}
fullTargets: [{type: range, excludeStart: false, excludeEnd: false, start: {type: primitive, mark: {type: cursorToken}, selectionType: token, position: contents, modifier: {type: identity}, insideOutsideType: inside}, end: {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: t}, selectionType: token, position: contents, modifier: {type: identity}, insideOutsideType: inside}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
spokenForm: take trap past token
languageId: typescript
command:
actionName: setSelection
partialTargets:
- type: range
start:
type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: t}
end: {type: primitive, selectionType: token}
excludeStart: false
excludeEnd: false
extraArgs: []
marks:
default.t:
start: {line: 0, character: 6}
end: {line: 0, character: 11}
initialState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
finalState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 11}
active: {line: 0, character: 0}
thatMark:
- anchor: {line: 0, character: 11}
active: {line: 0, character: 0}
fullTargets: [{type: range, excludeStart: false, excludeEnd: false, start: {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: t}, selectionType: token, position: contents, modifier: {type: identity}, insideOutsideType: inside}, end: {type: primitive, mark: {type: cursorToken}, selectionType: token, position: contents, modifier: {type: identity}, insideOutsideType: inside}}]
32 changes: 32 additions & 0 deletions src/test/suite/fixtures/recorded/positions/chuckPastAfterLook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
spokenForm: chuck past after look
languageId: typescript
command:
actionName: delete
partialTargets:
- type: range
start: {type: primitive}
end:
type: primitive
position: after
mark: {type: decoratedSymbol, symbolColor: default, character: l}
excludeStart: false
excludeEnd: false
extraArgs: []
marks:
default.l:
start: {line: 0, character: 0}
end: {line: 0, character: 5}
initialState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
finalState:
documentContents: hethere
selections:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
thatMark:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
fullTargets: [{type: range, excludeStart: false, excludeEnd: false, start: {type: primitive, mark: {type: cursor}, selectionType: token, position: contents, modifier: {type: identity}, insideOutsideType: outside}, end: {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: l}, selectionType: token, position: after, modifier: {type: identity}, insideOutsideType: outside}}]
32 changes: 32 additions & 0 deletions src/test/suite/fixtures/recorded/positions/chuckPastBeforeTrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
spokenForm: chuck past before trap
languageId: typescript
command:
actionName: delete
partialTargets:
- type: range
start: {type: primitive}
end:
type: primitive
position: before
mark: {type: decoratedSymbol, symbolColor: default, character: t}
excludeStart: false
excludeEnd: false
extraArgs: []
marks:
default.t:
start: {line: 0, character: 6}
end: {line: 0, character: 11}
initialState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
finalState:
documentContents: helloere
selections:
- anchor: {line: 0, character: 5}
active: {line: 0, character: 5}
thatMark:
- anchor: {line: 0, character: 5}
active: {line: 0, character: 5}
fullTargets: [{type: range, excludeStart: false, excludeEnd: false, start: {type: primitive, mark: {type: cursor}, selectionType: token, position: contents, modifier: {type: identity}, insideOutsideType: outside}, end: {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: t}, selectionType: token, position: before, modifier: {type: identity}, insideOutsideType: outside}}]
26 changes: 26 additions & 0 deletions src/test/suite/fixtures/recorded/positions/chuckPastEndOfLine.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: chuck past end of line
languageId: typescript
command:
actionName: delete
partialTargets:
- type: range
start: {type: primitive}
end: {type: primitive, position: after, insideOutsideType: inside, selectionType: line}
excludeStart: false
excludeEnd: false
extraArgs: []
marks: {}
initialState:
documentContents: hello there
selections:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
finalState:
documentContents: he
selections:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
thatMark:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
fullTargets: [{type: range, excludeStart: false, excludeEnd: false, start: {type: primitive, mark: {type: cursor}, selectionType: token, position: contents, modifier: {type: identity}, insideOutsideType: outside}, end: {type: primitive, mark: {type: cursor}, selectionType: line, position: after, modifier: {type: identity}, insideOutsideType: inside}}]
Loading