Skip to content

Commit

Permalink
Persist global marks on disk
Browse files Browse the repository at this point in the history
  • Loading branch information
EerikSaksi committed Aug 28, 2023
1 parent 6b667ec commit 870e952
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion extensionBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { globalState } from './src/state/globalState';
import { Register } from './src/register/register';
import { SpecialKeys } from './src/util/specialKeys';
import { exCommandParser } from './src/vimscript/exCommandParser';
import { HistoryStep } from './src/history/historyTracker';

let extensionContext: vscode.ExtensionContext;
let previousActiveEditorUri: vscode.Uri | undefined;
Expand Down Expand Up @@ -107,7 +108,7 @@ export async function activate(context: vscode.ExtensionContext, handleLocal: bo

// Load state
Register.loadFromDisk(handleLocal);
await Promise.all([ExCommandLine.loadHistory(context), SearchCommandLine.loadHistory(context)]);
await Promise.all([ExCommandLine.loadHistory(context), SearchCommandLine.loadHistory(context), HistoryStep.loadHistory(context)]);

if (vscode.window.activeTextEditor) {
const filepathComponents = vscode.window.activeTextEditor.document.fileName.split(/\\|\//);
Expand Down
6 changes: 6 additions & 0 deletions src/history/historyFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ export class CommandLineHistory extends HistoryFile {
super(context, '.cmdline_history');
}
}

export class MarkHistory extends HistoryFile {
constructor(context: ExtensionContext) {
super(context, '.mark_history');
}
}
24 changes: 23 additions & 1 deletion src/history/historyTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { Mode } from '../mode/mode';
import { ErrorCode, VimError } from '../error';
import { Logger } from '../util/logger';
import { earlierOf } from '../common/motion/position';
import { MarkHistory } from './historyFile';
import { ExtensionContext } from 'vscode';

const diffEngine = new DiffMatchPatch.diff_match_patch();
diffEngine.Diff_Timeout = 1; // 1 second
Expand Down Expand Up @@ -106,7 +108,7 @@ export interface IMark {
/**
* An undo's worth of changes; generally corresponds to a single action.
*/
class HistoryStep {
export class HistoryStep {
/**
* The insertions and deletions that occured in this history step.
*/
Expand Down Expand Up @@ -145,6 +147,23 @@ class HistoryStep {
*/
static globalMarks: IMark[] = [];

public static markHistory: MarkHistory;

public static async loadHistory(context: ExtensionContext): Promise<void> {
HistoryStep.markHistory = new MarkHistory(context);
await HistoryStep.markHistory.load();
HistoryStep.markHistory.get().forEach((row) => {
const parsed = JSON.parse(row);
const newMark : IMark = {
position: parsed.position,
name: parsed.name,
isUppercaseMark: true,
document: parsed.document
};
HistoryStep.globalMarks.push(newMark);
});
}

constructor(init: { marks: IMark[]; changes?: DocumentChange[]; cameFromU?: boolean }) {
this.changes = init.changes ?? [];
this.marks = init.marks ?? [];
Expand Down Expand Up @@ -552,6 +571,9 @@ export class HistoryTracker {
marks[previousIndex] = mark;
} else {
marks.push(mark);
}
if (mark.isUppercaseMark) {
HistoryStep.markHistory.add(JSON.stringify(mark));
}
}

Expand Down

0 comments on commit 870e952

Please sign in to comment.