|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 3 | + * See 'LICENSE' in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +import * as fs from 'fs'; |
| 8 | +import * as path from 'path'; |
| 9 | + |
| 10 | +export const cachedEditorConfigSettings: Map<string, any> = new Map<string, any>(); |
| 11 | + |
| 12 | +export function mapIndentationReferenceToEditorConfig(value: string | undefined): string { |
| 13 | + if (value !== undefined) { |
| 14 | + // Will never actually be undefined, as these settings have default values. |
| 15 | + if (value === "statementBegin") { |
| 16 | + return "statement_begin"; |
| 17 | + } |
| 18 | + if (value === "outermostParenthesis") { |
| 19 | + return "outermost_parenthesis"; |
| 20 | + } |
| 21 | + } |
| 22 | + return "innermost_parenthesis"; |
| 23 | +} |
| 24 | + |
| 25 | +export function mapIndentToEditorConfig(value: string | undefined): string { |
| 26 | + if (value !== undefined) { |
| 27 | + // Will never actually be undefined, as these settings have default values. |
| 28 | + if (value === "leftmostColumn") { |
| 29 | + return "leftmost_column"; |
| 30 | + } |
| 31 | + if (value === "oneLeft") { |
| 32 | + return "one_left"; |
| 33 | + } |
| 34 | + } |
| 35 | + return "none"; |
| 36 | +} |
| 37 | + |
| 38 | +export function mapNewOrSameLineToEditorConfig(value: string | undefined): string { |
| 39 | + if (value !== undefined) { |
| 40 | + // Will never actually be undefined, as these settings have default values. |
| 41 | + if (value === "newLine") { |
| 42 | + return "new_line"; |
| 43 | + } |
| 44 | + if (value === "sameLine") { |
| 45 | + return "same_line"; |
| 46 | + } |
| 47 | + } |
| 48 | + return "ignore"; |
| 49 | +} |
| 50 | + |
| 51 | +export function mapWrapToEditorConfig(value: string | undefined): string { |
| 52 | + if (value !== undefined) { |
| 53 | + // Will never actually be undefined, as these settings have default values. |
| 54 | + if (value === "allOneLineScopes") { |
| 55 | + return "all_one_line_scopes"; |
| 56 | + } |
| 57 | + if (value === "oneLiners") { |
| 58 | + return "one_liners"; |
| 59 | + } |
| 60 | + } |
| 61 | + return "never"; |
| 62 | +} |
| 63 | + |
| 64 | +function matchesSection(filePath: string, section: string): boolean { |
| 65 | + const fileName: string = path.basename(filePath); |
| 66 | + // Escape all regex special characters except '*' and '?'. |
| 67 | + // Convert wildcards '*' to '.*' and '?' to '.'. |
| 68 | + const sectionPattern = section.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.'); |
| 69 | + const regex: RegExp = new RegExp(`^${sectionPattern}$`); |
| 70 | + return regex.test(fileName); |
| 71 | +} |
| 72 | + |
| 73 | +function parseEditorConfigContent(content: string): Record<string, any> { |
| 74 | + const lines = content.split(/\r?\n/); |
| 75 | + const config: Record<string, any> = {}; |
| 76 | + let currentSection: string | null = '*'; // Use '*' for sectionless (global) settings. |
| 77 | + |
| 78 | + lines.forEach(line => { |
| 79 | + line = line.trim(); |
| 80 | + |
| 81 | + if (!line || line.startsWith('#') || line.startsWith(';')) { |
| 82 | + // Skip empty lines and comments. |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (line.startsWith('[') && line.endsWith(']')) { |
| 87 | + // New section (e.g., [*.js]) |
| 88 | + currentSection = line.slice(1, -1).trim(); |
| 89 | + config[currentSection] = config[currentSection] || {}; |
| 90 | + } else { |
| 91 | + // Key-value pair (e.g., indent_style = space). |
| 92 | + const [key, ...values] = line.split('='); |
| 93 | + if (key && values.length > 0) { |
| 94 | + const trimmedKey = key.trim(); |
| 95 | + const value = values.join('=').trim(); |
| 96 | + if (currentSection) { |
| 97 | + // Ensure the current section is initialized. |
| 98 | + if (!config[currentSection]) { |
| 99 | + config[currentSection] = {}; |
| 100 | + } |
| 101 | + config[currentSection][trimmedKey] = value; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + return config; |
| 108 | +} |
| 109 | + |
| 110 | +function getEditorConfig(filePath: string): any { |
| 111 | + let combinedConfig: any = {}; |
| 112 | + let globalConfig: any = {}; |
| 113 | + let currentDir: string = path.dirname(filePath); |
| 114 | + const rootDir: string = path.parse(currentDir).root; |
| 115 | + |
| 116 | + // Traverse from the file's directory to the root directory. |
| 117 | + for (;;) { |
| 118 | + const editorConfigPath: string = path.join(currentDir, '.editorconfig'); |
| 119 | + if (fs.existsSync(editorConfigPath)) { |
| 120 | + const configFileContent: string = fs.readFileSync(editorConfigPath, 'utf-8'); |
| 121 | + const configData = parseEditorConfigContent(configFileContent); |
| 122 | + |
| 123 | + // Extract global (sectionless) entries. |
| 124 | + if (configData['*']) { |
| 125 | + globalConfig = { |
| 126 | + ...globalConfig, |
| 127 | + ...configData['*'] |
| 128 | + }; |
| 129 | + } |
| 130 | + |
| 131 | + // Match sections and combine configurations. |
| 132 | + Object.keys(configData).forEach((section: string) => { |
| 133 | + if (section !== '*' && matchesSection(filePath, section)) { |
| 134 | + combinedConfig = { |
| 135 | + ...combinedConfig, |
| 136 | + ...configData[section] |
| 137 | + }; |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + // Check if the current .editorconfig is the root. |
| 142 | + if (configData['*']?.root?.toLowerCase() === 'true') { |
| 143 | + break; // Stop searching after processing the root = true file. |
| 144 | + } |
| 145 | + } |
| 146 | + if (currentDir === rootDir) { |
| 147 | + break; // Stop the loop after checking the root directory. |
| 148 | + } |
| 149 | + currentDir = path.dirname(currentDir); |
| 150 | + } |
| 151 | + |
| 152 | + // Merge global config with section-based config. |
| 153 | + return { |
| 154 | + ...globalConfig, |
| 155 | + ...combinedConfig |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +// Look up the appropriate .editorconfig settings for the specified file. |
| 160 | +// This is intentionally not async to avoid races due to multiple entrancy. |
| 161 | +export function getEditorConfigSettings(fsPath: string): Promise<any> { |
| 162 | + let editorConfigSettings: any = cachedEditorConfigSettings.get(fsPath); |
| 163 | + if (!editorConfigSettings) { |
| 164 | + editorConfigSettings = getEditorConfig(fsPath); |
| 165 | + cachedEditorConfigSettings.set(fsPath, editorConfigSettings); |
| 166 | + } |
| 167 | + return editorConfigSettings; |
| 168 | +} |
0 commit comments