]> Git Repo - vscode-clang-format.git/commitdiff
Refactor to easily add new languages and settings master
authorJesse Taube <[email protected]>
Wed, 17 Jul 2024 05:43:38 +0000 (01:43 -0400)
committerJesse Taube <[email protected]>
Sat, 3 Aug 2024 18:45:03 +0000 (14:45 -0400)
Adds setting for a list of supported languages.
Also adds an object for language specific style and fallbackStyle.

Signed-off-by: Jesse Taube <[email protected]>
package.json
src/clangMode.ts [deleted file]
src/extension.ts

index 4ba848e154d99bfe609ce1ac1e93ead0060d77cb..82c9e87d27c5cf54d18e95ff99370546e5cb27bc 100644 (file)
   ],
   "main": "./out/src/extension",
   "activationEvents": [
-    "onLanguage:cpp",
-    "onLanguage:c",
-    "onLanguage:csharp",
-    "onLanguage:objective-c",
-    "onLanguage:objective-cpp",
-    "onLanguage:java",
-    "onLanguage:javascript",
-    "onLanguage:typescript",
-    "onLanguage:proto",
-    "onLanguage:proto3",
-    "onLanguage:textproto",
-    "onLanguage:apex",
-    "onLanguage:glsl",
-    "onLanguage:hlsl",
-    "onLanguage:cuda",
-    "onLanguage:cuda-cpp"
+    "onStartupFinished"
   ],
   "contributes": {
     "configuration": {
       "title": "Clang-Format configuration",
       "properties": {
         "clang-format.executable": {
-          "type": "string",
-          "default": "clang-format",
+          "additionalProperties": false,
+          "order": 1,
+          "type": "object",
+          "properties": {
+            "default": {
+              "type": "string",
+              "default": "clang-format"
+            },
+            "windows": {
+              "type": "string"
+            },
+            "linux": {
+              "type": "string"
+            },
+            "osx": {
+              "type": "string"
+            }
+          },
+          "default": {
+            "default": "clang-format"
+          },
           "description": "clang-format executable path"
         },
-        "clang-format.executable.windows": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format executable path on Windows"
-        },
-        "clang-format.executable.linux": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format executable path on Linux"
-        },
-        "clang-format.executable.osx": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format executable path on macOS"
-        },
-        "clang-format.style": {
-          "type": "string",
-          "default": "file",
-          "description": "clang-format style.(-style=value, value can be file, LLVM, Google, Chromium, Mozilla, WebKit or json configure)"
-        },
-        "clang-format.fallbackStyle": {
+        "clang-format.style.default": {
+          "order": 2,
           "type": "string",
+          "title": "Style",
           "default": "LLVM",
-          "description": "clang-format fallback style.(-fallback-style=value, value can be none, LLVM, Google, Chromium, Mozilla, WebKit)"
-        },
-        "clang-format.language.cpp.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for C++ (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.cpp.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for C++, left empty to use clang-format.style"
-        },
-        "clang-format.language.cpp.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for C++, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.c.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for C (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.c.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for C, left empty to use clang-format.style"
-        },
-        "clang-format.language.c.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for C, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.csharp.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for C# (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.csharp.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for C#, left empty to use clang-format.style"
-        },
-        "clang-format.language.csharp.fallbackStyle": {
-          "type": "string",
-          "default": "Microsoft",
-          "description": "clang-format fallback style for C#, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.objective-c.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for Objective-C (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.objective-c.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Objective-C, left empty to use clang-format.style"
-        },
-        "clang-format.language.objective-c.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Objective-C, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.objective-cpp.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for Objective-C++ (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.objective-cpp.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Objective-C++, left empty to use clang-format.style"
-        },
-        "clang-format.language.objective-cpp.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Objective-C++, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.java.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for Java (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.java.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Java, left empty to use clang-format.style"
-        },
-        "clang-format.language.java.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Java, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.javascript.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for JavaScript (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.javascript.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for JavaScript, left empty to use clang-format.style"
-        },
-        "clang-format.language.javascript.fallbackStyle": {
-          "type": "string",
-          "default": "google",
-          "description": "clang-format fallback style for JavaScript"
-        },
-        "clang-format.language.typescript.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for TypeScript (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.typescript.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for TypeScript, left empty to use clang-format.style"
-        },
-        "clang-format.language.typescript.fallbackStyle": {
-          "type": "string",
-          "default": "google",
-          "description": "clang-format fallback style for TypeScript"
-        },
-        "clang-format.language.proto.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for Protobuf (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.proto.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Protobuf, left empty to use clang-format.style"
-        },
-        "clang-format.language.proto.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Protobuf, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.textproto.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for textproto files"
-        },
-        "clang-format.language.textproto.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for textproto, left empty to use clang-format.style"
-        },
-        "clang-format.language.textproto.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for textproto, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.apex.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for Apex (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.apex.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Apex, left empty to use clang-format.style"
-        },
-        "clang-format.language.apex.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for Apex, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.glsl.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for GLSL (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.glsl.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for GLSL, left empty to use clang-format.style"
-        },
-        "clang-format.language.glsl.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for GLSL, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.hlsl.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for HLSL (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.hlsl.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for HLSL, left empty to use clang-format.style"
-        },
-        "clang-format.language.hlsl.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for HLSL, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.cuda.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for CUDA (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.cuda.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for CUDA, left empty to use clang-format.style"
-        },
-        "clang-format.language.cuda.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for CUDA, left empty to use clang-format.fallbackStyle"
-        },
-        "clang-format.language.cuda-cpp.enable": {
-          "type": "boolean",
-          "default": true,
-          "description": "enable formatting for CUDA (requires reloading Visual Studio Code)"
-        },
-        "clang-format.language.cuda-cpp.style": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for CUDA, left empty to use clang-format.style"
-        },
-        "clang-format.language.cuda-cpp.fallbackStyle": {
-          "type": "string",
-          "default": "",
-          "description": "clang-format fallback style for CUDA, left empty to use clang-format.fallbackStyle"
+          "description": "clang-format style.\n(-style=value, value can be file, LLVM, Google, Chromium, Mozilla, WebKit or json configure)"
+        },
+        "clang-format.fallbackStyle.default": {
+          "order": 3,
+          "type": "string",
+          "title": "Fallback Style",
+          "default": "",
+          "description": "clang-format fallback style.\n(-fallback-style=value, value can be none, LLVM, Google, Chromium, Mozilla, WebKit)"
+        },
+        "clang-format.style.languages": {
+          "order": 6,
+          "type": "object",
+          "default": {},
+          "additionalProperties": {
+            "type": "string"
+          },
+          "description": "Object with the key as LanguageId and value as a clang-format style."
+        },
+        "clang-format.fallbackStyle.languages": {
+          "order": 7,
+          "type": "object",
+          "default": {},
+          "additionalProperties": {
+            "type": "string"
+          },
+          "description": "Object with the key as LanguageId and value as a clang-format fallback style."
+        },
+        "clang-format.enabledLanguageIds": {
+          "order": 4,
+          "description": "Specify a list of languageIds to format.",
+          "title": "Enabled Language Ids",
+          "type": "array",
+          "items": {
+            "type": "string"
+          },
+          "default": [
+            "c"
+          ],
+          "uniqueItems": true
         },
         "clang-format.assumeFilename": {
+          "order": 99,
           "type": "string",
           "default": "",
-          "description": "When reading from stdin, clang-format assumes this filename to look for a style config file (with -style=file) and to determine the language."
+          "description": "When reading from stdin, clang-format assumes this filename to look for a style config file (with -style=file) and to determine the language.\nThe default value is the current filename being formated."
         },
         "clang-format.additionalArguments": {
+          "order": 100,
           "type": "string",
           "default": "",
           "description": "Additional arguments to pass to clang-format"
     "url": "https://github.com/xaverh/vscode-clang-format-provider"
   },
   "license": "MIT"
-}
+}
\ No newline at end of file
diff --git a/src/clangMode.ts b/src/clangMode.ts
deleted file mode 100644 (file)
index 5c16021..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-'use strict';
-
-import * as vscode from 'vscode';
-
-export const ALIAS = {
-  'proto3': 'proto'
-};
-
-let languages: string[] = [];
-for (let l of ['cpp', 'c', 'csharp', 'objective-c', 'objective-cpp', 'java', 'javascript', 'json', 'typescript', 'proto', 'proto3', 'textproto', 'apex', 'glsl', 'hlsl', 'cuda', 'cuda-cpp']) {
-  let confKey = `language.${ALIAS[l] || l}.enable`;
-  if (vscode.workspace.getConfiguration('clang-format').get(confKey)) {
-    languages.push(l);
-  }
-}
-
-export const MODES: vscode.DocumentFilter[] = languages.map((language) => ({language, scheme: 'file'}));
index aa54c1078472754bb00bc781ec9d01b6e6e41257..0a1f562badfce11190ed1bcad2aec3067f8875b8 100644 (file)
@@ -2,10 +2,6 @@ import * as vscode from 'vscode';
 import cp = require('child_process');
 import path = require('path');
 import fs = require('fs');
-import {
-  MODES,
-  ALIAS
-} from './clangMode';
 import { getBinPath } from './clangPath';
 import sax = require('sax');
 
@@ -143,10 +139,12 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
   private getExecutablePath() {
     const platform = getPlatformString();
     const config = vscode.workspace.getConfiguration('clang-format');
+    const ExecPath = config.get<object>('executable');
+    let execPath = ExecPath[platform];
 
-    const platformExecPath = config.get<string>('executable.' + platform);
-    const defaultExecPath = config.get<string>('executable');
-    let execPath = platformExecPath || defaultExecPath;
+    if (!execPath) {
+      execPath = ExecPath['default'];
+    }
 
     if (!execPath) {
       return this.defaultConfigure.executable;
@@ -156,7 +154,6 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
     if (execPath.includes('${workspaceFolder})')) {
       execPath = execPath.replace('${workspaceFolder}', this.getWorkspaceFolder());
     }
-
     return execPath
       .replace(/\${workspaceRoot}/g, vscode.workspace.rootPath)
       .replace(/\${cwd}/g, process.cwd())
@@ -166,12 +163,13 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
   }
 
   private getLanguage(document: vscode.TextDocument): string {
-    return ALIAS[document.languageId] || document.languageId;
+    return document.languageId;
   }
 
   private getStyle(document: vscode.TextDocument) {
     const config = vscode.workspace.getConfiguration('clang-format');
-    let ret = config.get<string>(`language.${this.getLanguage(document)}.style`);
+    const styleLangs = config.get<string>('style.languages');
+    let ret = styleLangs[this.getLanguage(document)];
 
     if (ret && ret.trim()) {
       ret = this.replaceStyleVariables(ret.trim(), document);
@@ -180,7 +178,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
       }
     }
 
-    ret = config.get<string>('style');
+    ret = config.get<string>('style.default');
     if (ret && ret.trim()) {
       ret = this.replaceStyleVariables(ret.trim(), document);
       if (ret && ret.trim()) {
@@ -193,7 +191,8 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
 
   private getFallbackStyle(document: vscode.TextDocument) {
     const config = vscode.workspace.getConfiguration('clang-format');
-    let strConf = config.get<string>(`language.${this.getLanguage(document)}.fallbackStyle`);
+    const fallbackStyleLangs = config.get<object>('fallbackStyle.languages');
+    let strConf = fallbackStyleLangs[this.getLanguage(document)];
 
     if (strConf && strConf.trim()) {
       strConf = this.replaceStyleVariables(strConf.trim(), document);
@@ -202,7 +201,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
       }
     }
 
-    strConf = config.get<string>('fallbackStyle');
+    strConf = config.get<string>('fallbackStyle.default');
     if (strConf && strConf.trim()) {
       strConf = this.replaceStyleVariables(strConf.trim(), document);
       if (strConf && strConf.trim()) {
@@ -362,12 +361,14 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
 
 export function activate(ctx: vscode.ExtensionContext): void {
 
+  const config = vscode.workspace.getConfiguration('clang-format');
   const formatter = new ClangDocumentFormattingEditProvider();
-  let availableLanguages = {};
+  const availableLanguages = vscode.languages.getLanguages();
+  const enabledLangs = config.get<string[]>('enabledLanguageIds');
 
-  MODES.forEach((mode) => {
+  enabledLangs.forEach((language) => {
+    let mode = { language, scheme: 'file' };
     ctx.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider(mode, formatter));
     ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(mode, formatter));
-    availableLanguages[mode.language] = true;
   });
 }
This page took 0.0581199999999999 seconds and 4 git commands to generate.