【问题标题】:How to get autoClosingPairs for current document in vscode如何在 vscode 中获取当前文档的 autoClosingPairs
【发布时间】:2020-12-13 23:57:22
【问题描述】:

我想获得通过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 文档,但找不到解决方案。

谢谢。

【问题讨论】:

    标签: visual-studio-code vscode-settings vscode-extensions


    【解决方案1】:

    似乎还没有 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's。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 2019-05-24
      • 2021-08-30
      • 1970-01-01
      相关资源
      最近更新 更多