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

Treat line numbers as proper marks #180

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 12 additions & 11 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,24 @@ export interface DecoratedSymbol {
character: string;
}

export interface LineNumberPosition {
lineNumber: number;
isRelative: boolean;
}
export interface LineNumber {
type: "lineNumber";
anchor: LineNumberPosition;
active: LineNumberPosition;
}

export type Mark =
| CursorMark
| CursorMarkToken
| That
| Source
| LastCursorPosition
| DecoratedSymbol;
| DecoratedSymbol
| LineNumber;
export type Delimiter =
| "squareBrackets"
| "curlyBrackets"
Expand Down Expand Up @@ -103,15 +114,6 @@ export interface SubpieceModifier {
export interface MatchingPairSymbolModifier {
type: "matchingPairSymbol";
}
export interface LineNumberModifierPosition {
lineNumber: number;
isRelative: boolean;
}
export interface LineNumberModifier {
type: "lineNumber";
anchor: LineNumberModifierPosition;
active: LineNumberModifierPosition;
}
export interface IdentityModifier {
type: "identity";
}
Expand All @@ -128,7 +130,6 @@ export type Modifier =
| ContainingScopeModifier
| SubpieceModifier
| MatchingPairSymbolModifier
| LineNumberModifier
| HeadModifier
| TailModifier;

Expand Down
53 changes: 27 additions & 26 deletions src/processTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Target,
TypedSelection,
Modifier,
LineNumberModifierPosition,
LineNumberPosition,
} from "./Types";
import { performInsideOutsideAdjustment } from "./performInsideOutsideAdjustment";
import { SUBWORD_MATCHER } from "./constants";
Expand Down Expand Up @@ -188,6 +188,11 @@ function getSelectionsFromMark(
switch (mark.type) {
case "cursor":
return context.currentSelections;
case "that":
return context.thatMark;
case "source":
return context.sourceMark;

case "cursorToken": {
const tokens = context.currentSelections.map((selection) => {
const token = context.navigationMap.getTokenForRange(
Expand All @@ -203,6 +208,7 @@ function getSelectionsFromMark(
editor: token.editor,
}));
}

case "decoratedSymbol":
const token = context.navigationMap.getToken(
mark.symbolColor,
Expand All @@ -219,10 +225,26 @@ function getSelectionsFromMark(
editor: token.editor,
},
];
case "that":
return context.thatMark;
case "source":
return context.sourceMark;

case "lineNumber": {
const getLine = (linePosition: LineNumberPosition) =>
linePosition.isRelative
? context.currentEditor!.selection.active.line +
linePosition.lineNumber
: linePosition.lineNumber;
return [
{
selection: new Selection(
getLine(mark.anchor),
0,
getLine(mark.active),
0
),
editor: context.currentEditor!,
},
];
}

case "lastCursorPosition":
throw new Error("Not implemented");
}
Expand Down Expand Up @@ -368,27 +390,6 @@ function transformSelection(
];
}

case "lineNumber": {
const getLine = (linePosition: LineNumberModifierPosition) =>
linePosition.isRelative
? selection.editor.selection.active.line + linePosition.lineNumber
: linePosition.lineNumber;
return [
{
selection: update(selection, {
selection: () =>
new Selection(
getLine(modifier.anchor),
0,
getLine(modifier.active),
0
),
}),
context: {},
},
];
}

case "matchingPairSymbol":
case "surroundingPair":
throw new Error("Not implemented");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ command:
partialTargets:
- type: primitive
selectionType: line
modifier:
mark:
type: lineNumber
anchor: {lineNumber: 1, isRelative: true}
active: {lineNumber: 1, isRelative: true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ command:
partialTargets:
- type: primitive
selectionType: line
modifier:
mark:
type: lineNumber
anchor: {lineNumber: 1, isRelative: true}
active: {lineNumber: 3, isRelative: true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ command:
partialTargets:
- type: primitive
selectionType: line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this selectionType comes from talon side? Probably ok but came as a bit of a surprise. I wonder if we could infer that info somewhere in the extension, either during inference or processing

Copy link
Member Author

@AndreasArvidsson AndreasArvidsson Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could infer it extension side if we want. But that is true for several other commands as well. This solution has the upside that we don't add additional code we don't actually need to. Instead we reuse the functionality of the line selector and the mark actually only selects the first character of the correct line.

modifier:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we add a test case that uses it like a proper mark? Eg "take funk row five"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really have that for the other marks. There is no "take funk that" or similar. But it is treated like the other marks in the code so there really is no difference. If we should add more test for marks in general is a different question.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd like to have that kind of test, but if you think it's better to do those in one pass that's fine too; we don't need to block this PR on that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly I think that we probably should do proper tests for all the marks as a separate thing and not just this one specifically.

mark:
type: lineNumber
anchor: {lineNumber: 3, isRelative: false}
active: {lineNumber: 3, isRelative: false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ command:
partialTargets:
- type: primitive
selectionType: line
modifier:
mark:
type: lineNumber
anchor: {lineNumber: 1, isRelative: false}
active: {lineNumber: 3, isRelative: true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ command:
partialTargets:
- type: primitive
selectionType: line
modifier:
mark:
type: lineNumber
anchor: {lineNumber: 1, isRelative: false}
active: {lineNumber: 3, isRelative: false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ command:
partialTargets:
- type: primitive
selectionType: line
modifier:
mark:
type: lineNumber
anchor: {lineNumber: -1, isRelative: true}
active: {lineNumber: 1, isRelative: true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ command:
partialTargets:
- type: primitive
selectionType: line
modifier:
mark:
type: lineNumber
anchor: {lineNumber: -1, isRelative: true}
active: {lineNumber: 3, isRelative: false}
Expand Down