Я хотел бы получить значение autoClosingPairs
, которое определяется через language-configuration.json
для каждого языка.
{
"comments": {
"lineComment": "//",
"blockComment": [ "/*", "*/" ]
},
"brackets": [
["{", "}"],
["[", "]"],
["(", ")"]
],
"autoClosingPairs": [
{ "open": "{", "close": "}" },
{ "open": "[", "close": "]" },
{ "open": "(", "close": ")" },
{ "open": "'", "close": "'", "notIn": ["string", "comment"] },
{ "open": "\"", "close": "\"", "notIn": ["string"] },
{ "open": "`", "close": "`", "notIn": ["string", "comment"] },
{ "open": "/**", "close": " */", "notIn": ["string"] }
],
"autoCloseBefore": ";:.,=}])>` \n\t",
"surroundingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["'", "'"],
["\"", "\""],
["`", "`"]
],
"folding": {
"markers": {
"start": "^\\s*//\\s*#?region\\b",
"end": "^\\s*//\\s*#?endregion\\b"
}
},
"wordPattern": "(-?\\d*\\.\\d\\w*)|([^\\`\\~\\!\\@\\#\\%\\^\\&\\*\\(\\)\\-\\=\\+\\[\\{\\]\\}\\\\\\|\\;\\:\\'\\\"\\,\\.\\<\\>\/\\?\\s]+)",
"indentationRules": {
"increaseIndentPattern": "^((?!.*?\/\\*).*\\*/)?\\s*[\\}\\]].*$",
"decreaseIndentPattern": "^((?!\/\/).)*(\\{[^}\"'`]*|\\([^)\"'`]*|\\[[^\\]\"'`]*)$"
}
}
Закрывает вещи, которые я собрал вместе workspace.getConfiguration(languageId);
и extensions.getExtension('vscode.typescript-language-features')
, но ни один из них не имеет значения autoClosingPairs. Прочитал документацию по API, но не смог найти решение.
Спасибо.
Похоже, пока нет API для получения значений языковой конфигурации, таких как autoClosingPairs
, см. https://github.com/Microsoft/vscode/issues/2871 в бэклоге.
Но в этой проблеме есть обходной путь:
const fs = require('fs');
const path = require('path');
// in some function or `activate`
const editor = vscode.window.activeTextEditor;
const documentLanguageId = editor.document.languageId;
var langConfigFilepath = null;
for (const _ext of vscode.extensions.all) {
// All vscode default extensions ids starts with "vscode."
if (
_ext.id.startsWith("vscode.") &&
_ext.packageJSON.contributes &&
_ext.packageJSON.contributes.languages
) {
// Find language data from "packageJSON.contributes.languages" for the
// current file's languageId (or just use them all and don't filter here
const packageLangData = _ext.packageJSON.contributes.languages.find(
_packageLangData => (_packageLangData.id === documentLanguageId)
);
// If found, get the absolute config file path
if (!!packageLangData) {
langConfigFilepath = path.join(
_ext.extensionPath,
packageLangData.configuration
);
break;
}
}
}
// Validate config file existence
if (!!langConfigFilepath && fs.existsSync(langConfigFilepath)) {
let langConfig = require(langConfigFilepath);
let aCPs = langConfig.autoClosingPairs; // if you prefer/can use this route
// or use this
let aCPs2 = JSON.parse(fs.readFileSync(langConfigFilepath).toString()).autoClosingPairs;
}
с небольшими изменениями, сделанными мной для javascript и использования autoClosingPairs
в последнем if
теле.
Спасибо https://github.com/Microsoft/vscode/issues/2871#issuecomment-338364014
Как написано, он получает файл конфигурации языка для activeTextEditor
. Вы можете изменить его, чтобы зациклить желаемые documentLanguageId
.