Skip to content

Simplify storage directory to use supplied extension directory #4151

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

Merged
merged 10 commits into from
Oct 27, 2019
3 changes: 3 additions & 0 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export async function activate(context: vscode.ExtensionContext) {
extensionContext = context;
extensionContext.subscriptions.push(StatusBar);

// Set the storage path to be used by history files
Globals.extensionStoragePath = context.globalStoragePath;

if (vscode.window.activeTextEditor) {
const filepathComponents = vscode.window.activeTextEditor.document.fileName.split(/\\|\//);
Register.putByKey(filepathComponents[filepathComponents.length - 1], '%', undefined, true);
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -856,10 +856,8 @@
"postinstall": "node ./node_modules/vscode/bin/install"
},
"dependencies": {
"appdirectory": "0.1.0",
"diff-match-patch": "1.0.4",
"lodash.escaperegexp": "4.1.2",
"mkdirp": "0.5.1",
"neovim": "4.5.0",
"untildify": "4.0.0",
"winston": "3.2.1",
Expand Down
2 changes: 2 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export class Globals {
static mockModeHandler: ModeHandler;

static mockConfiguration: IConfiguration;

static extensionStoragePath: string;
}
35 changes: 19 additions & 16 deletions src/history/historyFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@ import * as fs from 'fs';
import * as path from 'path';
import { Logger } from '../util/logger';
import { configuration } from '../configuration/configuration';
import { getExtensionDirPath } from '../util/util';
import { promisify } from 'util';

const mkdirp = require('mkdirp');
import { Globals } from './../globals';

export class HistoryFile {
private readonly _logger = Logger.get('HistoryFile');
private _historyFileName: string;
private _historyDir: string;
private _history: string[] = [];

public get historyFilePath(): string {
return path.join(this._historyDir, this._historyFileName);
return path.join(Globals.extensionStoragePath, this._historyFileName);
}

constructor(historyFileName: string, historyDir?: string) {
constructor(historyFileName: string) {
this._historyFileName = historyFileName;
this._historyDir = historyDir ? historyDir : getExtensionDirPath();
}

public async add(value: string | undefined): Promise<void> {
Expand Down Expand Up @@ -69,9 +65,9 @@ export class HistoryFile {
data = await promisify(fs.readFile)(this.historyFilePath, 'utf-8');
} catch (err) {
if (err.code === 'ENOENT') {
this._logger.debug(`History does not exist. path=${this._historyDir}`);
this._logger.debug(`History does not exist. path=${this.historyFilePath}`);
} else {
this._logger.warn(`Failed to load history. path=${this._historyDir} err=${err}.`);
this._logger.warn(`Failed to load history. path=${this.historyFilePath} err=${err}.`);
}
return;
}
Expand All @@ -87,15 +83,22 @@ export class HistoryFile {
}
this._history = parsedData;
} catch (e) {
this._logger.warn(`Deleting corrupted history file. path=${this._historyDir} err=${e}.`);
this._logger.warn(`Deleting corrupted history file. path=${this.historyFilePath} err=${e}.`);
this.clear();
}
}

private async save(): Promise<void> {
try {
// create supplied directory. if directory already exists, do nothing.
await promisify(mkdirp)(this._historyDir, 0o775);
// create supplied directory. if directory already exists, do nothing and move on
try {
await promisify(fs.mkdir)(Globals.extensionStoragePath, { recursive: true });
} catch (createDirectoryErr) {
if (createDirectoryErr.code !== 'EEXIST') {
throw createDirectoryErr;
}
}

// create file
await promisify(fs.writeFile)(this.historyFilePath, JSON.stringify(this._history), 'utf-8');
} catch (err) {
Expand All @@ -106,13 +109,13 @@ export class HistoryFile {
}

export class SearchHistory extends HistoryFile {
constructor(historyFileDir?: string) {
super('.search_history', historyFileDir);
constructor() {
super('.search_history');
}
}

export class CommandLineHistory extends HistoryFile {
constructor(historyFileDir?: string) {
super('.cmdline_history', historyFileDir);
constructor() {
super('.cmdline_history');
}
}
1 change: 0 additions & 1 deletion src/state/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { SearchHistory } from '../history/historyFile';
import { SearchState, SearchDirection } from './searchState';
import { SubstituteState } from './substituteState';
import { configuration } from '../configuration/configuration';
import { VimState } from './vimState';

/**
* State which stores global state (across editors)
Expand Down
10 changes: 0 additions & 10 deletions src/util/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as vscode from 'vscode';
import AppDirectory = require('appdirectory');
import { Logger } from './logger';
import { Position } from '../common/motion/position';
import { Range } from '../common/motion/range';
Expand Down Expand Up @@ -38,15 +37,6 @@ export async function getCursorsAfterSync(timeoutInMilliseconds: number = 0): Pr
);
}

export function getExtensionDirPath(): string {
const logger = Logger.get('getExtensionDirPath');
const dirs = new AppDirectory('VSCodeVim');

logger.debug('VSCodeVim Cache Directory: ' + dirs.userCache());

return dirs.userCache();
}

/**
* This function execute a shell command and return the standard output as string.
*/
Expand Down
5 changes: 3 additions & 2 deletions test/cmd_line/historyFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import * as os from 'os';
import { HistoryFile } from '../../src/history/historyFile';
import { assertEqual, setupWorkspace, cleanUpWorkspace, rndName } from '../testUtils';
import { configuration } from '../../src/configuration/configuration';
import { Globals } from '../../src/globals';

suite('HistoryFile', () => {
let history: HistoryFile;
let run_cmds: string[];
const tmpDir = os.tmpdir();

const assertArrayEquals = (expected: any[], actual: any[]) => {
assertEqual(expected.length, actual.length);
Expand All @@ -25,7 +25,8 @@ suite('HistoryFile', () => {
run_cmds.push(i.toString());
}

history = new HistoryFile(rndName(), tmpDir);
Globals.extensionStoragePath = os.tmpdir();
history = new HistoryFile(rndName());
await history.load();
});

Expand Down