如何获取 vscode 中当前文档的 autoClosingPairs

How to get autoClosingPairs for current document in vscode

我想获得通过 language-configuration.json 为每种语言定义的 autoClosingPairs 值。

{
    "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;
}

我对最后 if 正文中的 javascript 和 autoClosingPairs 用法稍作修改。

感谢https://github.com/Microsoft/vscode/issues/2871#issuecomment-338364014

如所写,它获取 activeTextEditor 的语言配置文件。您可以将其更改为循环播放您想要的 documentLanguageId