Skip to content

Commit

Permalink
feat: support scoped localesPaths setting (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed Aug 30, 2021
1 parent 9572e73 commit dbdf1b5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,8 @@
"items": {
"type": "string"
},
"description": "%config.locales_paths%"
"description": "%config.locales_paths%",
"scope": "resource"
},
"i18n-ally.encoding": {
"type": "string",
Expand Down
21 changes: 18 additions & 3 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { execSync } from 'child_process'
import { workspace, extensions, ExtensionContext, commands } from 'vscode'
import { workspace, extensions, ExtensionContext, commands, ConfigurationScope, WorkspaceFolder } from 'vscode'
import { trimEnd, uniq } from 'lodash'
import { TagSystems } from '../tagSystems'
import { EXT_NAMESPACE, EXT_ID, EXT_LEGACY_NAMESPACE, KEY_REG_DEFAULT, KEY_REG_ALL, DEFAULT_LOCALE_COUNTRY_MAP } from '../meta'
Expand Down Expand Up @@ -276,6 +276,21 @@ export class Config {
this.setConfig('localesPaths', paths)
}

static getLocalesPathsInScope(scope: WorkspaceFolder): string[] | undefined {
const paths = this.getConfig('localesPaths', scope)

let localesPaths: string[]
if (!paths)
return
else if (typeof paths === 'string')
localesPaths = paths.split(',')
else
localesPaths = paths
if (!localesPaths)
return
return localesPaths.map(i => trimEnd(i, '/\\').replace(/\\/g, '/'))
}

static updateLocalesPaths(paths: string[]) {
this._localesPaths = uniq((this._localesPaths || []).concat(paths))
}
Expand Down Expand Up @@ -490,9 +505,9 @@ export class Config {
}

// config
private static getConfig<T = any>(key: string): T | undefined {
private static getConfig<T = any>(key: string, scope?: ConfigurationScope | undefined): T | undefined {
let config = workspace
.getConfiguration(EXT_NAMESPACE)
.getConfiguration(EXT_NAMESPACE, scope)
.get<T>(key)

// compatible to vue-i18n-ally
Expand Down
15 changes: 12 additions & 3 deletions src/core/Global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { extname, resolve } from 'path'
import { workspace, commands, window, EventEmitter, Event, ExtensionContext, ConfigurationChangeEvent, TextDocument } from 'vscode'
import { workspace, commands, window, EventEmitter, Event, ExtensionContext, ConfigurationChangeEvent, TextDocument, WorkspaceFolder } from 'vscode'
import { uniq } from 'lodash'
import { slash } from '@antfu/utils'
import { isMatch } from 'micromatch'
Expand All @@ -25,6 +25,7 @@ export class Global {
private static _loaders: Record<string, LocaleLoader> = {}
private static _rootpath: string
private static _enabled = false
private static _currentWorkspaceFolder: WorkspaceFolder

static context: ExtensionContext
static enabledFrameworks: Framework[] = []
Expand Down Expand Up @@ -243,7 +244,13 @@ export class Global {
}

static get localesPaths(): string[] | undefined {
let config = Config._localesPaths
let config

if (this._currentWorkspaceFolder)
config = Config.getLocalesPathsInScope(this._currentWorkspaceFolder)
else
config = Config._localesPaths

if (!config) {
config = this.enabledFrameworks.flatMap(f => f.perferredLocalePaths || [])
if (!config.length)
Expand Down Expand Up @@ -288,8 +295,10 @@ export class Global {
const resource = editor.document.uri
if (resource.scheme === 'file') {
const folder = workspace.getWorkspaceFolder(resource)
if (folder)
if (folder) {
this._currentWorkspaceFolder = folder
rootpath = folder.uri.fsPath
}
}

if (!rootpath && workspace.rootPath)
Expand Down

0 comments on commit dbdf1b5

Please sign in to comment.