Skip to content

Commit

Permalink
Migrate to only custom formats
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths committed Feb 9, 2024
1 parent 8dd8559 commit 313f6a4
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 17 deletions.
1 change: 0 additions & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
**/*.zip
node_modules/**
out/**
out/test/**
package-lock.json
res/*.svg
src/**
Expand Down
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@
}
]
},
"activationEvents": [
"onCommand:guid.insert",
"onCommand:guid.insertMany",
"onCommand:guid.insertEmpty"
],
"icon": "res/logo.png",
"galleryBanner": {
"color": "#252526",
Expand All @@ -93,13 +88,13 @@
"watch": "webpack --watch",
"watch-test": "tsc -p . -w",
"pretest": "npm run compile-test",
"test": "node ./out/test/runTest.js",
"test": "node ./out/src/test/runTest.js",
"package": "webpack --mode production --devtool hidden-source-map",
"preversion": "npm test",
"vscode:prepublish": "npm run package"
},
"engines": {
"vscode": "^1.30.0"
"vscode": "^1.75.0"
},
"dependencies": {
"buffer": "^6.0.3",
Expand Down
16 changes: 8 additions & 8 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import * as vscode from 'vscode';
import * as util from 'util';
import { Guid } from './guid';
import settings from './settings';

enum FormatType {
LOWERCASE,
Expand Down Expand Up @@ -192,15 +193,13 @@ export function insertEmptyCommand(textEditor: vscode.TextEditor, edit: vscode.T

async function insertCommandImpl(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, type: GuidGenerateType): Promise<void> {
const g = type === GuidGenerateType.EMPTY ? Guid.EMPTY : new Guid();
const settings = vscode.workspace.getConfiguration('insertGuid');
const showLowercase = settings.get<boolean>('showLowercase', true);
const showUppercase = settings.get<boolean>('showUppercase', false);
const showCodeSnippets = settings.get<boolean>('showCodeSnippets', true);
const pasteAutomatically = settings.get<string>('pasteAutomatically', '');

const items = getQuickPickItems(g, showLowercase, showUppercase, showCodeSnippets);
let item = items[0];
const _settings = settings();
const showLowercase = _settings.showLowercase;
const showUppercase = _settings.showUppercase;
const showCodeSnippets = _settings.showCodeSnippets;
const pasteAutomatically = _settings.pasteAutomatically;

let item: GuidPickItem;
if (pasteAutomatically !== '') {
// Format with the specified string and insert without user selection
const customFormatter = {
Expand All @@ -210,6 +209,7 @@ async function insertCommandImpl(textEditor: vscode.TextEditor, edit: vscode.Tex
item = new GuidPickItem(-1, g, customFormatter)
} else {
// Let user select format
const items = getQuickPickItems(g, showLowercase, showUppercase, showCodeSnippets);
const selection = await vscode.window.showQuickPick<GuidPickItem>(items)
if (selection == null) {
// Selection canceled.
Expand Down
88 changes: 88 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// The MIT License (MIT)
//
// Copyright (c) Heath Stewart
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import { WorkspaceConfiguration, workspace } from 'vscode';

export interface Settings {
showLowercase: boolean
showUppercase: boolean
showCodeSnippets: boolean
pasteAutomatically: string
formats: string | undefined
};

export const DEFAULTS: Settings = {
showLowercase: true,
showUppercase: false,
showCodeSnippets: true,
pasteAutomatically: '',
formats: undefined,
};

class Value<T> {
constructor(value: T | undefined, defaultValue: T) {
this.value = value;
this.overridden = value !== defaultValue;
this.valueOrDefault = () => {
return this.value ?? defaultValue;
}
}

readonly value: T | undefined;
readonly overridden: boolean;
readonly valueOrDefault: () => T;
}

class SettingsImpl implements Settings {
private readonly _settings: WorkspaceConfiguration;
constructor() {
this._settings = workspace.getConfiguration('insertGuid');
}

get showLowercase(): boolean {
return this.get('showLowercase', DEFAULTS.showLowercase).valueOrDefault();
}

get showUppercase(): boolean {
return this.get('showUppercase', DEFAULTS.showUppercase).valueOrDefault();
}

get showCodeSnippets(): boolean {
return this.get('showCodeSnippets', DEFAULTS.showCodeSnippets).valueOrDefault();
}

get pasteAutomatically(): string {
return this.get('pasteAutomatically', DEFAULTS.pasteAutomatically).valueOrDefault();
}

get formats(): string | undefined {
// TODO
return undefined;
}

private get<T>(section: string, defaultValue: T): Value<T> {
const value = this._settings.get<T>(section);
return new Value(value, defaultValue);
}
};

export default (): Settings => new SettingsImpl();
35 changes: 35 additions & 0 deletions src/test/suite/settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// The MIT License (MIT)
//
// Copyright (c) Heath Stewart
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import * as assert from 'assert';
import { contributes } from '../../../package.json';
import { DEFAULTS } from '../../settings';

suite('Settings', () => {
test('DEFAULTS matches package.json configuration defaults', () => {
const properties = contributes.configuration.properties;
assert.strictEqual(properties['insertGuid.showLowercase'].default, DEFAULTS.showLowercase);
assert.strictEqual(properties['insertGuid.showUppercase'].default, DEFAULTS.showUppercase);
assert.strictEqual(properties['insertGuid.showCodeSnippets'].default, DEFAULTS.showCodeSnippets);
assert.strictEqual(properties['insertGuid.pasteAutomatically'].default, DEFAULTS.pasteAutomatically);
})
});
10 changes: 9 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
],
"strict": true,
"sourceMap": true,
"rootDir": "src",
"rootDirs": [
"src",
"."
],
"outDir": "out",
"resolveJsonModule": true,
"removeComments": true,
"noUnusedLocals": true
},
"include": [
"src/**/*",
"package.json"
],
"exclude": [
"node_modules",
".vscode-test",
Expand Down

0 comments on commit 313f6a4

Please sign in to comment.