|
1 | 1 | import * as nls from 'vscode-nls' |
| 2 | +import { Uri, workspace, window } from 'vscode' |
2 | 3 | import { existsSync, readFileSync } from 'fs' |
3 | 4 | import { parse } from 'jsonc-parser' |
4 | | -import { join, resolve } from 'path' |
5 | | -import { Uri, workspace } from 'vscode' |
| 5 | +import { join, resolve, dirname } from 'path' |
| 6 | +import type { ExtensionContext } from 'vscode' |
6 | 7 | import type { Config, Line } from './types' |
7 | 8 |
|
8 | 9 | export let conf!: Config |
9 | | -export const eufemiaConfigFileName = '.vscode-eufemia.json' |
| 10 | +export const configFileName = '.vscode-eufemia.json' |
10 | 11 |
|
11 | 12 | export const localize = nls.config({ |
12 | 13 | messageFormat: nls.MessageFormat.both, |
13 | 14 | })() |
14 | 15 |
|
15 | | -function loadConfigViaFile() { |
16 | | - if ( |
17 | | - !workspace.workspaceFolders || |
18 | | - workspace.workspaceFolders?.length <= 0 |
19 | | - ) { |
20 | | - return |
21 | | - } |
| 16 | +let CACHE_CONFIG_DIR_PATH: Record<string, string | null> = {} |
22 | 17 |
|
23 | | - const eufemiaConfigPath = join( |
24 | | - workspace.workspaceFolders[0].uri.fsPath, |
25 | | - eufemiaConfigFileName |
| 18 | +export function loadConfigFromFile() { |
| 19 | + const activeFilePath = dirname( |
| 20 | + window.activeTextEditor?.document.fileName || '' |
26 | 21 | ) |
27 | 22 |
|
28 | | - if (!existsSync(eufemiaConfigPath)) { |
29 | | - console.warn(`File not found: ${eufemiaConfigPath}`) |
30 | | - return |
| 23 | + let configDirPath = (CACHE_CONFIG_DIR_PATH?.[activeFilePath] || |
| 24 | + null) as string |
| 25 | + |
| 26 | + if (!configDirPath && activeFilePath) { |
| 27 | + const paths = activeFilePath.split(/\/+|\\+/g) |
| 28 | + |
| 29 | + for (let i = 0, l = paths.length; i < l; i++) { |
| 30 | + const path = resolve(...paths) |
| 31 | + |
| 32 | + if ( |
| 33 | + existsSync(join(path, configFileName)) || |
| 34 | + // Skip on package.json too, so we not always run this on every file over again (when in same directory) |
| 35 | + existsSync(join(path, 'package.json')) |
| 36 | + ) { |
| 37 | + configDirPath = CACHE_CONFIG_DIR_PATH[activeFilePath] = path |
| 38 | + break |
| 39 | + } |
| 40 | + |
| 41 | + paths.pop() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + const configFilePath = join(configDirPath, configFileName) |
| 46 | + |
| 47 | + if (!existsSync(configFilePath)) { |
| 48 | + return // stop here |
31 | 49 | } |
32 | 50 |
|
33 | 51 | try { |
34 | | - const res = parse(readFileSync(eufemiaConfigPath).toString('utf-8')) |
| 52 | + const res = parse(readFileSync(configFilePath, 'utf-8')) |
35 | 53 | conf = { |
36 | 54 | ...conf, |
37 | 55 | ...res, |
38 | 56 | } |
39 | | - console.warn(`Use override config via ${eufemiaConfigPath} file`) |
40 | | - } catch (ex) { |
41 | | - console.warn(`Parse error in ${eufemiaConfigPath}`, ex) |
| 57 | + console.info(`Using config from file: ${configFilePath}`) |
| 58 | + } catch (e) { |
| 59 | + console.warn(`Parse error in ${configFilePath}`, e) |
42 | 60 | } |
43 | 61 | } |
44 | 62 |
|
@@ -77,9 +95,25 @@ function setConfig() { |
77 | 95 | conf = tmp as unknown as Config |
78 | 96 | } |
79 | 97 |
|
| 98 | +export function initConfig(context: ExtensionContext) { |
| 99 | + loadConfig() |
| 100 | + |
| 101 | + workspace.onDidChangeConfiguration(loadConfig) |
| 102 | + workspace.onDidOpenTextDocument(loadConfigFromFile) |
| 103 | + |
| 104 | + const configWatcher = workspace.createFileSystemWatcher( |
| 105 | + `**/${configFileName}` |
| 106 | + ) |
| 107 | + |
| 108 | + configWatcher.onDidChange(loadConfig) |
| 109 | + configWatcher.onDidCreate(loadConfig) |
| 110 | + configWatcher.onDidDelete(loadConfig) |
| 111 | + |
| 112 | + context.subscriptions.push(configWatcher) |
| 113 | +} |
| 114 | + |
80 | 115 | export function loadConfig() { |
81 | 116 | setConfig() |
82 | | - loadConfigViaFile() |
83 | 117 | initIngores() |
84 | 118 | initLanguages() |
85 | 119 | } |
|
0 commit comments