Skip to content

Commit d26bce4

Browse files
committed
refactor(sfc): composed loadder
1 parent a08a5b6 commit d26bce4

File tree

14 files changed

+131
-39
lines changed

14 files changed

+131
-39
lines changed

src/commands/dev.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { commands, window } from 'vscode'
22
import { Commands } from '../core'
33
import { ExtensionModule } from '../modules'
4-
import { SFCLoader } from '../core/SfcLoader'
4+
import { SFCLoader } from '../core/loaders/SfcLoader'
55

66
const m: ExtensionModule = (ctx) => {
77
return [
@@ -12,7 +12,7 @@ const m: ExtensionModule = (ctx) => {
1212
return
1313
if (editor.document.languageId !== 'vue')
1414
return
15-
const loader = new SFCLoader(editor.document.uri.fsPath)
15+
const loader = new SFCLoader(editor.document.uri)
1616
loader.load()
1717
}),
1818
]

src/core/CurrentFile.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { workspace, ExtensionContext, Uri, window } from 'vscode'
2+
import { ComposedLoader } from './loaders/ComposedLoader'
3+
import { Global } from './Global'
4+
import { SFCLoader } from './loaders/SfcLoader'
5+
import { Loader } from '.'
6+
7+
export class CurrentFile {
8+
static _sfc_loader: SFCLoader | null = null
9+
10+
static watch (ctx: ExtensionContext) {
11+
ctx.subscriptions.push(workspace.onDidSaveTextDocument(e => this.update(e.uri)))
12+
ctx.subscriptions.push(window.onDidChangeActiveTextEditor(e => this.update(e && e.document.uri)))
13+
14+
this.update(window.activeTextEditor && window.activeTextEditor.document.uri)
15+
}
16+
17+
static update (uri?: Uri) {
18+
if (!Global.enabled)
19+
return
20+
if (this._sfc_loader) {
21+
if (uri && this._sfc_loader.uri.path === uri.path) {
22+
this._sfc_loader.load()
23+
return
24+
}
25+
else {
26+
this._sfc_loader.dispose()
27+
this._sfc_loader = null
28+
}
29+
}
30+
if (uri && uri.fsPath.endsWith('.vue'))
31+
this._sfc_loader = new SFCLoader(uri)
32+
}
33+
34+
static get loader () {
35+
const loaders: Loader[] = [Global.loader]
36+
if (this._sfc_loader)
37+
loaders.push(this._sfc_loader)
38+
39+
return new ComposedLoader(loaders)
40+
}
41+
}

src/core/Translator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { EventEmitter } from 'vscode'
22
import { google, baidu, youdao } from 'translation.js'
33
import { TranslateResult } from 'translation.js/declaration/api/types'
44
import { Log } from '../utils'
5-
import { LocaleLoader } from './LocaleLoader'
65
import { AllyError, ErrorType } from './Errors'
76
import { Global } from './Global'
87
import { LocaleTree } from './types'
9-
import { LocaleNode, LocaleRecord, Config } from '.'
8+
import { LocaleLoader, LocaleNode, LocaleRecord, Config } from '.'
109

1110
interface TranslatorChangeEvent {
1211
keypath: string

src/core/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export * from './types'
22
export * from './Commands'
33
export * from './Global'
4-
export * from './LocaleLoader'
54
export * from './KeyDetector'
65
export * from './Config'
7-
export * from './BaseLoader'
8-
export * from './SfcLoader'
6+
export * from './CurrentFile'
7+
export * from './loaders/Loader'
8+
export * from './loaders/SfcLoader'
9+
export * from './loaders/LocaleLoader'

src/core/loaders/ComposedLoader.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import _ from 'lodash'
2+
import { LocaleNode, LocaleTree } from '../types'
3+
import { Loader } from './Loader'
4+
5+
export class ComposedLoader extends Loader {
6+
constructor (
7+
public loaders: Loader[] = []
8+
) {
9+
super('[Composed]')
10+
}
11+
12+
get root (): LocaleTree {
13+
const children: Record<string | number, LocaleTree | LocaleNode> = {}
14+
for (const loader of this.loaders) {
15+
const loaderChildren = loader.root.children
16+
for (const key of Object.keys(loaderChildren))
17+
children[key] = loaderChildren[key]
18+
}
19+
return new LocaleTree({ keypath: '', children })
20+
}
21+
22+
get locales (): string[] {
23+
return _(this.loaders)
24+
.flatMap(l => l.locales)
25+
.uniq()
26+
.value()
27+
}
28+
29+
getShadowFilePath (keypath: string, locale: string) {
30+
for (const loader of this.loaders.reverse()) {
31+
const value = loader.getShadowFilePath(keypath, locale)
32+
if (value)
33+
return value
34+
}
35+
}
36+
37+
getTreeNodeByKey (keypath: string, tree?: LocaleTree) {
38+
for (const loader of this.loaders.reverse()) {
39+
const value = loader.getTreeNodeByKey(keypath, tree)
40+
if (value)
41+
return value
42+
}
43+
}
44+
}

src/core/BaseLoader.ts renamed to src/core/loaders/Loader.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Disposable } from 'vscode'
2-
import { Log } from '../utils'
3-
import { LocaleTree, LocaleNode, LocaleRecord } from './types'
4-
import { Config, Global } from '.'
2+
import { Log } from '../../utils'
3+
import { LocaleTree, LocaleNode, LocaleRecord } from '../types'
4+
import { Config, Global } from '..'
55

6-
export abstract class BaseLoader extends Disposable {
6+
export abstract class Loader extends Disposable {
77
protected _disposables: Disposable[] = []
88

99
constructor (

src/core/LocaleLoader.ts renamed to src/core/loaders/LocaleLoader.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import * as path from 'path'
33
import * as _ from 'lodash'
44
import { workspace, window, EventEmitter, WorkspaceEdit } from 'vscode'
55
import * as fg from 'fast-glob'
6-
import { replaceLocalePath, normalizeLocale, Log } from '../utils'
7-
import i18n from '../i18n'
8-
import { Analyst } from '../analysis/Analyst'
9-
import { LocaleTree, ParsedFile, FlattenLocaleTree, Coverage, LocaleNode, LocaleRecord, PendingWrite } from './types'
10-
import { AllyError, ErrorType } from './Errors'
11-
import { Translator } from './Translator'
12-
import { BaseLoader } from './BaseLoader'
13-
import { Global, Config } from '.'
14-
15-
export class LocaleLoader extends BaseLoader {
6+
import { replaceLocalePath, normalizeLocale, Log } from '../../utils'
7+
import i18n from '../../i18n'
8+
import { Analyst } from '../../analysis/Analyst'
9+
import { LocaleTree, ParsedFile, FlattenLocaleTree, Coverage, LocaleNode, LocaleRecord, PendingWrite } from '../types'
10+
import { AllyError, ErrorType } from '../Errors'
11+
import { Translator } from '../Translator'
12+
import { Loader } from './Loader'
13+
import { Global, Config } from '..'
14+
15+
export class LocaleLoader extends Loader {
1616
private _onDidChange = new EventEmitter<undefined>()
1717

1818
readonly onDidChange = this._onDidChange.event

src/core/SfcLoader.ts renamed to src/core/loaders/SfcLoader.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { extname } from 'path'
22
import { workspace, Uri } from 'vscode'
33
import { parse, HTMLElement } from 'node-html-parser'
4-
import { BaseLoader } from './BaseLoader'
5-
import { LocaleTree } from './types'
6-
import { Global } from './Global'
4+
import { Log } from '../../utils'
5+
import { LocaleTree } from '../types'
6+
import { Global } from '../Global'
7+
import { Loader } from './Loader'
78

89
interface Section {
910
lang: string
@@ -17,13 +18,15 @@ interface ParseredSection extends Section{
1718
data: any
1819
}
1920

20-
export class SFCLoader extends BaseLoader {
21+
export class SFCLoader extends Loader {
2122
_root: LocaleTree = new LocaleTree({ keypath: '' })
2223

2324
constructor (
24-
public readonly filepath: string
25+
public readonly uri: Uri
2526
) {
26-
super(`[SFC]${filepath}`)
27+
super(`[SFC]${uri.fsPath}`)
28+
29+
this.load()
2730
}
2831

2932
private getSections (text: string): Section[] {
@@ -65,7 +68,9 @@ export class SFCLoader extends BaseLoader {
6568
_parsedSections: ParseredSection[] = []
6669

6770
async load () {
68-
const doc = await workspace.openTextDocument(Uri.file(this.filepath))
71+
Log.info(`📑 Loading sfc ${this.uri.fsPath}`)
72+
this._parsedSections = []
73+
const doc = await workspace.openTextDocument(this.uri)
6974
const sections = this.getSections(doc.getText())
7075
for (const section of sections) {
7176
const data = await this.loadSection(section)
@@ -85,6 +90,6 @@ export class SFCLoader extends BaseLoader {
8590
}
8691

8792
getShadowFilePath (keypath: string, locale: string) {
88-
return this.filepath
93+
return this.uri.fsPath
8994
}
9095
}

src/editor/annotation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { window, DecorationOptions, Range, workspace, Disposable } from 'vscode'
22
import { debounce } from 'lodash'
3-
import { Global, KeyDetector, Config, BaseLoader } from '../core'
3+
import { Global, KeyDetector, Config, Loader } from '../core'
44
import { ExtensionModule } from '../modules'
55

66
const noneDecorationType = window.createTextEditorDecorationType({})
@@ -18,7 +18,7 @@ const annotation: ExtensionModule = (ctx) => {
1818
if (!activeTextEditor)
1919
return
2020

21-
const loader: BaseLoader = Global.loader // TODO:sfc
21+
const loader: Loader = Global.loader // TODO:sfc
2222
const document = activeTextEditor.document
2323
const annotations: DecorationOptions[] = []
2424
const underlines: DecorationOptions[] = []

src/editor/completion.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode'
2-
import { Global, KeyDetector, BaseLoader } from '../core'
2+
import { Global, KeyDetector, Loader } from '../core'
33
import { ExtensionModule } from '../modules'
44
import { LANG_SELECTORS } from '../meta'
55

@@ -11,7 +11,7 @@ class CompletionProvider implements vscode.CompletionItemProvider {
1111
if (!Global.enabled)
1212
return
1313

14-
const loader: BaseLoader = Global.loader // TODO:sfc
14+
const loader: Loader = Global.loader // TODO:sfc
1515
let key = KeyDetector.getKey(document, position)
1616
if (!key || !/\.$/.test(key))
1717
return

0 commit comments

Comments
 (0)