Skip to content

Commit

Permalink
Use l10n instead of vscode-nls (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhao271 committed Dec 21, 2022
1 parent f97dec8 commit 0c3e9f4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"url": "https://github.com/Microsoft/vscode-emmet-helper"
},
"devDependencies": {
"@types/vscode": "^1.73.0",
"@types/mocha": "^9.1.0",
"@types/node": "^14.0.0",
"mocha": "^9.1.0",
Expand All @@ -26,7 +27,6 @@
"jsonc-parser": "^2.3.0",
"vscode-languageserver-textdocument": "^1.0.1",
"vscode-languageserver-types": "^3.15.1",
"vscode-nls": "^5.0.0",
"vscode-uri": "^2.1.2"
},
"scripts": {
Expand Down
36 changes: 20 additions & 16 deletions src/emmetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@
*--------------------------------------------------------------------------------------------*/


import { Position, Range, CompletionItem, CompletionList, TextEdit, InsertTextFormat, CompletionItemKind } from 'vscode-languageserver-types'
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as JSONC from 'jsonc-parser';
import { cssData, htmlData } from './data';
import { URI } from 'vscode-uri';
import { FileService, joinPath, isAbsolutePath, FileType, FileStat } from './fileService';
import { TextDecoder } from 'util';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { CompletionItem, CompletionItemKind, CompletionList, InsertTextFormat, Position, Range, TextEdit } from 'vscode-languageserver-types';
import { URI } from 'vscode-uri';
import { cssData, htmlData } from './data';
import { FileService, FileStat, FileType, isAbsolutePath, joinPath } from './fileService';

import expand, { Config, extract, ExtractOptions, MarkupAbbreviation, Options, parseMarkup, parseStylesheet, resolveConfig, stringifyMarkup, stringifyStylesheet, StylesheetAbbreviation, SyntaxType, UserConfig } from 'emmet';
import { parseSnippets, SnippetsMap, syntaxes } from './configCompat';

import * as nls from 'vscode-nls';

const localize = nls.loadMessageBundle();

// /* workaround for webpack issue: https://github.com/webpack/webpack/issues/5756
// @emmetio/extract-abbreviation has a cjs that uses a default export
// */
// const extract = typeof _extractAbbreviation === 'function' ? _extractAbbreviation : _extractAbbreviation.default;

export { FileService, FileType, FileStat };

export { FileService, FileType, FileStat }
let l10n: { t: (message: string) => string };
try {
l10n = require('vscode').l10n;
} catch {
// Fallback to the identity function.
l10n = {
t: (message: string) => message
};
}

const snippetKeyCache = new Map<string, string[]>();
let markupSnippetKeys: string[];
Expand Down Expand Up @@ -129,7 +134,7 @@ export function doComplete(document: TextDocument, position: Position, syntax: s
expandedAbbr.textEdit = TextEdit.replace(abbreviationRange, escapeNonTabStopDollar(addFinalTabStop(expandedText)));
expandedAbbr.documentation = replaceTabStopsWithCursors(expandedText);
expandedAbbr.insertTextFormat = InsertTextFormat.Snippet;
expandedAbbr.detail = localize('Emmet abbreviation', "Emmet Abbreviation");
expandedAbbr.detail = l10n.t('Emmet abbreviation');
expandedAbbr.label = abbreviation;
expandedAbbr.label += filter ? '|' + filter.replace(',', '|') : "";
completionItems = [expandedAbbr];
Expand Down Expand Up @@ -837,7 +842,7 @@ function getVariables(variablesFromSettings: object | undefined): SnippetsMap {
if (!variablesFromSettings) {
return variablesFromFile;
}
return Object.assign({}, variablesFromFile, variablesFromSettings);
return Object.assign({}, variablesFromFile, variablesFromSettings) as SnippetsMap;
}

function getFormatters(syntax: string, preferences: any): any {
Expand Down Expand Up @@ -1023,7 +1028,7 @@ function updateVariables(varsJson: any) {
if (typeof varsJson === 'object' && varsJson) {
variablesFromFile = Object.assign({}, variablesFromFile, varsJson);
} else {
throw new Error(localize("emmetInvalidVariables", "Invalid emmet.variables field. See https://code.visualstudio.com/docs/editor/emmet#_emmet-configuration for a valid example."))
throw new Error(l10n.t('Invalid emmet.variables field. See https://code.visualstudio.com/docs/editor/emmet#_emmet-configuration for a valid example.'));
}
}

Expand All @@ -1035,7 +1040,7 @@ function updateProfiles(profileJson: any) {
if (typeof profileJson === 'object' && profileJson) {
profilesFromFile = Object.assign({}, profilesFromFile, profileJson);
} else {
throw new Error(localize("emmetInvalidProfiles", "Invalid syntax profile. See https://code.visualstudio.com/docs/editor/emmet#_emmet-configuration for a valid example."))
throw new Error(l10n.t('Invalid syntax profile. See https://code.visualstudio.com/docs/editor/emmet#_emmet-configuration for a valid example.'));
}
}

Expand Down Expand Up @@ -1072,11 +1077,10 @@ function updateSnippets(snippetsJson: any) {
const prevSnippetsRegistry = customSnippetsRegistry[syntax];
const newSnippets = parseSnippets(customSnippets);
const mergedSnippets = Object.assign({}, prevSnippetsRegistry, newSnippets);
const mergedSnippetKeys = Object.keys(mergedSnippets);
customSnippetsRegistry[syntax] = mergedSnippets;
});
} else {
throw new Error(localize("emmetInvalidSnippets", "Invalid snippets file. See https://code.visualstudio.com/docs/editor/emmet#_using-custom-emmet-snippets for a valid example."))
throw new Error(l10n.t('Invalid snippets file. See https://code.visualstudio.com/docs/editor/emmet#_using-custom-emmet-snippets for a valid example.'));
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/test/emmetHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Position, CompletionItemKind } from 'vscode-languageserver-types'
import { isAbbreviationValid, extractAbbreviation, extractAbbreviationFromText, getExpandOptions, emmetSnippetField, updateExtensionsPath as updateExtensionsPathHelper, doComplete, expandAbbreviation } from '../emmetHelper';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { describe, it } from 'mocha';
import assert from 'assert';
import { ExtractOptions } from 'emmet';
import * as fs from 'fs';
import { describe, it } from 'mocha';
import * as path from 'path';
import * as util from 'util';
import * as fs from 'fs';
import { FileService, FileType } from '../fileService';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { CompletionItemKind, Position } from 'vscode-languageserver-types';
import { URI } from 'vscode-uri';
import { ExtractOptions } from 'emmet';
import { doComplete, emmetSnippetField, expandAbbreviation, extractAbbreviation, extractAbbreviationFromText, getExpandOptions, isAbbreviationValid, updateExtensionsPath as updateExtensionsPathHelper } from '../emmetHelper';
import { FileService, FileType } from '../fileService';

const extensionsPath = [path.join(path.normalize(path.join(__dirname, '../../..')), 'testData', 'custom-snippets-profile')];
const bemFilterExample = 'ul.search-form._wide>li.-querystring+li.-btn_large';
Expand Down Expand Up @@ -1272,6 +1272,7 @@ describe('Test completions', () => {

assert.ok(completionList);
assert.strictEqual(completionList.items[0].kind, CompletionItemKind.Snippet);
assert.strictEqual(completionList.items[0].detail, 'Emmet abbreviation');
});

it('should not provide double completions for commonly used tags that are also snippets', async () => {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.18.0.tgz#98df2397f6936bfbff4f089e40e06fa5dd88d32a"
integrity sha512-0GeIl2kmVMXEnx8tg1SlG6Gg8vkqirrW752KqolYo1PHevhhZN3bhJ67qHj+bQaINhX0Ra3TlWwRvMCd9iEfNQ==

"@types/vscode@^1.73.0":
version "1.73.0"
resolved "https://registry.yarnpkg.com/@types/vscode/-/vscode-1.73.0.tgz#e34630cecc9eea1ae796fbb03d7c513a6571f960"
integrity sha512-FhkfF7V3fj7S3WqXu7AxFesBLO3uMkdCPJJPbwyZXezv2xJ6xBWHYM2CmkkbO8wT9Fr3KipwxGGOoQRrYq7mHg==

"@ungap/promise-all-settled@1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz#aa58042711d6e3275dd37dc597e5d31e8c290a44"
Expand Down Expand Up @@ -531,11 +536,6 @@ vscode-languageserver-types@^3.15.1:
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0.tgz#ecf393fc121ec6974b2da3efb3155644c514e247"
integrity sha512-k8luDIWJWyenLc5ToFQQMaSrqCHiLwyKPHKPQZ5zz21vM+vIVUSvsRpcbiECH4WR88K2XZqc4ScRcZ7nk/jbeA==

vscode-nls@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/vscode-nls/-/vscode-nls-5.0.0.tgz#99f0da0bd9ea7cda44e565a74c54b1f2bc257840"
integrity sha512-u0Lw+IYlgbEJFF6/qAqG2d1jQmJl0eyAGJHoAJqr2HT4M2BNuQYSEiSE75f52pXHSJm8AlTjnLLbBFPrdz2hpA==

vscode-uri@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-2.1.2.tgz#c8d40de93eb57af31f3c715dd650e2ca2c096f1c"
Expand Down

0 comments on commit 0c3e9f4

Please sign in to comment.