Skip to content

Commit 11d3b0c

Browse files
committed
move EmmyrcSchemaContentProvider
1 parent fa09925 commit 11d3b0c

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

src/emmyrcSchemaContentProvider.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as vscode from 'vscode';
2+
import * as path from 'path';
3+
import * as fs from 'fs';
4+
5+
/**
6+
* 提供`.emmyrc.json`的 i18n
7+
*/
8+
export class EmmyrcSchemaContentProvider implements vscode.TextDocumentContentProvider {
9+
private readonly schemaBaseDir: string;
10+
11+
constructor(context: vscode.ExtensionContext) {
12+
this.schemaBaseDir = path.join(context.extensionPath, 'syntaxes');
13+
}
14+
15+
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
16+
const schemaIdentifier = path.posix.basename(uri.path);
17+
const locale = vscode.env.language;
18+
let schemaFileName: string;
19+
20+
if (schemaIdentifier === 'emmyrc') {
21+
switch (locale) {
22+
case 'zh-cn':
23+
case 'zh-CN':
24+
case 'zh':
25+
schemaFileName = 'schema.zh-cn.json';
26+
break;
27+
case 'en':
28+
case 'en-US':
29+
case 'en-GB':
30+
default:
31+
schemaFileName = 'schema.json';
32+
break;
33+
}
34+
} else {
35+
return '';
36+
}
37+
38+
// 检查schema文件是否存在, 如果不存在则使用默认的
39+
let schemaFilePath = path.join(this.schemaBaseDir, schemaFileName);
40+
if (!fs.existsSync(schemaFilePath)) {
41+
schemaFilePath = path.join(this.schemaBaseDir, 'schema.json');
42+
}
43+
44+
try {
45+
return await fs.promises.readFile(schemaFilePath, 'utf8');
46+
} catch (error: any) {
47+
return JSON.stringify({
48+
"$schema": "https://json-schema.org/draft/2020-12/schema#",
49+
"title": "Error Loading Schema",
50+
"description": `Could not load schema: ${schemaFileName}. Error: ${error.message}.`
51+
});
52+
}
53+
}
54+
}

src/extension.ts

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { get } from './configRenames';
1818
import * as Annotator from './annotator';
1919
import { LuaRocksManager } from './luarocks/LuaRocksManager';
2020
import { LuaRocksTreeProvider, PackageTreeItem } from './luarocks/LuaRocksTreeProvider';
21+
import { EmmyrcSchemaContentProvider } from './emmyrcSchemaContentProvider';
2122
interface DebuggerConfig {
2223
readonly type: string;
2324
readonly provider: vscode.DebugConfigurationProvider;
@@ -558,57 +559,4 @@ async function checkLuaRocksInstallation(): Promise<void> {
558559
vscode.env.openExternal(vscode.Uri.parse('https://luarocks.org/#quick-start'));
559560
}
560561
}
561-
}
562-
563-
564-
565-
/**
566-
* 提供`.emmyrc.json`的 i18n
567-
*/
568-
class EmmyrcSchemaContentProvider implements vscode.TextDocumentContentProvider {
569-
private readonly schemaBaseDir: string;
570-
571-
constructor(context: vscode.ExtensionContext) {
572-
this.schemaBaseDir = path.join(context.extensionPath, 'syntaxes');
573-
}
574-
575-
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
576-
const schemaIdentifier = path.posix.basename(uri.path);
577-
const locale = vscode.env.language;
578-
let schemaFileName: string;
579-
580-
if (schemaIdentifier === 'emmyrc') {
581-
switch (locale) {
582-
case 'zh-cn':
583-
case 'zh-CN':
584-
case 'zh':
585-
schemaFileName = 'schema.zh-cn.json';
586-
break;
587-
case 'en':
588-
case 'en-US':
589-
case 'en-GB':
590-
default:
591-
schemaFileName = 'schema.json';
592-
break;
593-
}
594-
} else {
595-
return '';
596-
}
597-
598-
// 检查schema文件是否存在, 如果不存在则使用默认的
599-
let schemaFilePath = path.join(this.schemaBaseDir, schemaFileName);
600-
if (!fs.existsSync(schemaFilePath)) {
601-
schemaFilePath = path.join(this.schemaBaseDir, 'schema.json');
602-
}
603-
604-
try {
605-
return await fs.promises.readFile(schemaFilePath, 'utf8');
606-
} catch (error: any) {
607-
return JSON.stringify({
608-
"$schema": "https://json-schema.org/draft/2020-12/schema#",
609-
"title": "Error Loading Schema",
610-
"description": `Could not load schema: ${schemaFileName}. Error: ${error.message}.`
611-
});
612-
}
613-
}
614562
}

0 commit comments

Comments
 (0)