From 008a781b05ff3b9877322ca5efba083ed88f7ec7 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Thu, 5 Aug 2021 11:14:25 +0200 Subject: [PATCH] Treat line numbers as proper marks --- src/Types.ts | 23 ++++---- src/processTargets.ts | 53 ++++++++++--------- .../recorded/lineNumbers/takeDownOne.yml | 2 +- .../lineNumbers/takeDownOnePastThree.yml | 2 +- .../recorded/lineNumbers/takeRowFour.yml | 2 +- .../lineNumbers/takeRowTwoPastDownThree.yml | 2 +- .../lineNumbers/takeRowTwoPastFour.yml | 2 +- .../lineNumbers/takeUpOnePastDownOne.yml | 2 +- .../lineNumbers/takeUpOnePastRowFour.yml | 2 +- 9 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/Types.ts b/src/Types.ts index efa6abc6ca..04246ef232 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -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" @@ -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"; } @@ -128,7 +130,6 @@ export type Modifier = | ContainingScopeModifier | SubpieceModifier | MatchingPairSymbolModifier - | LineNumberModifier | HeadModifier | TailModifier; diff --git a/src/processTargets.ts b/src/processTargets.ts index 927d12b1fc..98df1a0ea1 100644 --- a/src/processTargets.ts +++ b/src/processTargets.ts @@ -13,7 +13,7 @@ import { Target, TypedSelection, Modifier, - LineNumberModifierPosition, + LineNumberPosition, } from "./Types"; import { performInsideOutsideAdjustment } from "./performInsideOutsideAdjustment"; import { SUBWORD_MATCHER } from "./constants"; @@ -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( @@ -203,6 +208,7 @@ function getSelectionsFromMark( editor: token.editor, })); } + case "decoratedSymbol": const token = context.navigationMap.getToken( mark.symbolColor, @@ -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"); } @@ -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"); diff --git a/src/test/suite/fixtures/recorded/lineNumbers/takeDownOne.yml b/src/test/suite/fixtures/recorded/lineNumbers/takeDownOne.yml index ce5ff3f42e..841576d14f 100644 --- a/src/test/suite/fixtures/recorded/lineNumbers/takeDownOne.yml +++ b/src/test/suite/fixtures/recorded/lineNumbers/takeDownOne.yml @@ -5,7 +5,7 @@ command: partialTargets: - type: primitive selectionType: line - modifier: + mark: type: lineNumber anchor: {lineNumber: 1, isRelative: true} active: {lineNumber: 1, isRelative: true} diff --git a/src/test/suite/fixtures/recorded/lineNumbers/takeDownOnePastThree.yml b/src/test/suite/fixtures/recorded/lineNumbers/takeDownOnePastThree.yml index 23e7c524e1..bf11f9aa60 100644 --- a/src/test/suite/fixtures/recorded/lineNumbers/takeDownOnePastThree.yml +++ b/src/test/suite/fixtures/recorded/lineNumbers/takeDownOnePastThree.yml @@ -5,7 +5,7 @@ command: partialTargets: - type: primitive selectionType: line - modifier: + mark: type: lineNumber anchor: {lineNumber: 1, isRelative: true} active: {lineNumber: 3, isRelative: true} diff --git a/src/test/suite/fixtures/recorded/lineNumbers/takeRowFour.yml b/src/test/suite/fixtures/recorded/lineNumbers/takeRowFour.yml index ba02b57c20..8a7173fd6b 100644 --- a/src/test/suite/fixtures/recorded/lineNumbers/takeRowFour.yml +++ b/src/test/suite/fixtures/recorded/lineNumbers/takeRowFour.yml @@ -5,7 +5,7 @@ command: partialTargets: - type: primitive selectionType: line - modifier: + mark: type: lineNumber anchor: {lineNumber: 3, isRelative: false} active: {lineNumber: 3, isRelative: false} diff --git a/src/test/suite/fixtures/recorded/lineNumbers/takeRowTwoPastDownThree.yml b/src/test/suite/fixtures/recorded/lineNumbers/takeRowTwoPastDownThree.yml index 00c2ffa96f..fa5f8becf8 100644 --- a/src/test/suite/fixtures/recorded/lineNumbers/takeRowTwoPastDownThree.yml +++ b/src/test/suite/fixtures/recorded/lineNumbers/takeRowTwoPastDownThree.yml @@ -5,7 +5,7 @@ command: partialTargets: - type: primitive selectionType: line - modifier: + mark: type: lineNumber anchor: {lineNumber: 1, isRelative: false} active: {lineNumber: 3, isRelative: true} diff --git a/src/test/suite/fixtures/recorded/lineNumbers/takeRowTwoPastFour.yml b/src/test/suite/fixtures/recorded/lineNumbers/takeRowTwoPastFour.yml index e3e8a698c5..9a7adcab80 100644 --- a/src/test/suite/fixtures/recorded/lineNumbers/takeRowTwoPastFour.yml +++ b/src/test/suite/fixtures/recorded/lineNumbers/takeRowTwoPastFour.yml @@ -5,7 +5,7 @@ command: partialTargets: - type: primitive selectionType: line - modifier: + mark: type: lineNumber anchor: {lineNumber: 1, isRelative: false} active: {lineNumber: 3, isRelative: false} diff --git a/src/test/suite/fixtures/recorded/lineNumbers/takeUpOnePastDownOne.yml b/src/test/suite/fixtures/recorded/lineNumbers/takeUpOnePastDownOne.yml index d0f8a1da21..6cca83ee66 100644 --- a/src/test/suite/fixtures/recorded/lineNumbers/takeUpOnePastDownOne.yml +++ b/src/test/suite/fixtures/recorded/lineNumbers/takeUpOnePastDownOne.yml @@ -5,7 +5,7 @@ command: partialTargets: - type: primitive selectionType: line - modifier: + mark: type: lineNumber anchor: {lineNumber: -1, isRelative: true} active: {lineNumber: 1, isRelative: true} diff --git a/src/test/suite/fixtures/recorded/lineNumbers/takeUpOnePastRowFour.yml b/src/test/suite/fixtures/recorded/lineNumbers/takeUpOnePastRowFour.yml index 482c60fd50..5d680c964a 100644 --- a/src/test/suite/fixtures/recorded/lineNumbers/takeUpOnePastRowFour.yml +++ b/src/test/suite/fixtures/recorded/lineNumbers/takeUpOnePastRowFour.yml @@ -5,7 +5,7 @@ command: partialTargets: - type: primitive selectionType: line - modifier: + mark: type: lineNumber anchor: {lineNumber: -1, isRelative: true} active: {lineNumber: 3, isRelative: false}