Skip to content

Commit fd92186

Browse files
authored
Add :ma[rk] (#9483)
1 parent fa5461b commit fd92186

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/cmd_line/commands/marks.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { QuickPickItem, window } from 'vscode';
22

33
// eslint-disable-next-line id-denylist
44
import { Parser, alt, noneOf, optWhitespace, regexp, seq, string, whitespace } from 'parsimmon';
5+
import { Position } from 'vscode';
56
import { Cursor } from '../../common/motion/cursor';
67
import { ErrorCode, VimError } from '../../error';
78
import { IMark } from '../../history/historyTracker';
89
import { VimState } from '../../state/vimState';
910
import { ExCommand } from '../../vimscript/exCommand';
11+
import { LineRange } from '../../vimscript/lineRange';
1012

1113
class MarkQuickPickItem implements QuickPickItem {
1214
mark: IMark;
@@ -133,3 +135,32 @@ export class DeleteMarksCommand extends ExCommand {
133135
vimState.historyTracker.removeMarks(marks);
134136
}
135137
}
138+
139+
export class MarkCommand extends ExCommand {
140+
public static readonly argParser: Parser<MarkCommand> = seq(
141+
optWhitespace,
142+
regexp(/[a-zA-Z'`<>[\].]/).desc('mark name'),
143+
optWhitespace,
144+
).map(([, markName]) => new MarkCommand(markName));
145+
146+
private markName: string;
147+
constructor(markName: string) {
148+
super();
149+
this.markName = markName;
150+
}
151+
152+
async execute(vimState: VimState): Promise<void> {
153+
const position = vimState.cursorStopPosition;
154+
vimState.historyTracker.addMark(vimState.document, position, this.markName);
155+
}
156+
157+
override async executeWithRange(vimState: VimState, range: LineRange): Promise<void> {
158+
/**
159+
* When a range is specified, the mark is set at the last line of the range.
160+
* For example, :1,5mark a will set mark 'a' at line 5.
161+
*/
162+
const { end } = range.resolve(vimState);
163+
const position = new Position(end, 0);
164+
vimState.historyTracker.addMark(vimState.document, position, this.markName);
165+
}
166+
}

src/vimscript/exCommandParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { GotoLineCommand } from '../cmd_line/commands/gotoLine';
1616
import { HistoryCommand } from '../cmd_line/commands/history';
1717
import { ClearJumpsCommand, JumpsCommand } from '../cmd_line/commands/jumps';
1818
import { CenterCommand, LeftCommand, RightCommand } from '../cmd_line/commands/leftRightCenter';
19-
import { DeleteMarksCommand, MarksCommand } from '../cmd_line/commands/marks';
19+
import { DeleteMarksCommand, MarksCommand, MarkCommand } from '../cmd_line/commands/marks';
2020
import { ExploreCommand } from '../cmd_line/commands/explore';
2121
import { MoveCommand } from '../cmd_line/commands/move';
2222
import { NohlCommand } from '../cmd_line/commands/nohl';
@@ -351,7 +351,7 @@ export const builtinExCommands: ReadonlyArray<[[string, string], ArgParser | und
351351
[['lvimgrepa', 'dd'], undefined],
352352
[['lw', 'indow'], succeed(new VsCodeCommand('workbench.action.focusCommentsPanel'))],
353353
[['m', 'ove'], MoveCommand.argParser],
354-
[['ma', 'rk'], undefined],
354+
[['ma', 'rk'], MarkCommand.argParser],
355355
[['mak', 'e'], undefined],
356356
[['map', ''], undefined],
357357
[['mapc', 'lear'], undefined],

test/vimscript/exCommandParse.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { GotoLineCommand } from '../../src/cmd_line/commands/gotoLine';
1010
import { HistoryCommand, HistoryCommandType } from '../../src/cmd_line/commands/history';
1111
import { LeftCommand, RightCommand } from '../../src/cmd_line/commands/leftRightCenter';
1212
import { LetCommand } from '../../src/cmd_line/commands/let';
13-
import { DeleteMarksCommand, MarksCommand } from '../../src/cmd_line/commands/marks';
13+
import { DeleteMarksCommand, MarksCommand, MarkCommand } from '../../src/cmd_line/commands/marks';
1414
import { PutExCommand } from '../../src/cmd_line/commands/put';
1515
import { QuitCommand } from '../../src/cmd_line/commands/quit';
1616
import { ReadCommand } from '../../src/cmd_line/commands/read';
@@ -382,6 +382,11 @@ suite('Ex command parsing', () => {
382382
exParseTest(':marks 0 1', new MarksCommand(['0', '1']));
383383
});
384384

385+
suite(':mark', () => {
386+
exParseTest(':mark a', new MarkCommand('a'));
387+
exParseTest(':mark `', new MarkCommand('`'));
388+
});
389+
385390
suite(':p[rint]', () => {
386391
// TODO
387392
});

0 commit comments

Comments
 (0)