Skip to content

Commit 697621d

Browse files
author
guqiankun.gqk
committed
feat: 迁移dt-sql
1 parent f6fd014 commit 697621d

33 files changed

Lines changed: 5259 additions & 101 deletions

packages/integrations/src/sql/index.tsx

Lines changed: 105 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import * as Alex from '@alipay/alex';
55
import '@alipay/alex/languages/sql';
66
import { isFilesystemReady } from '@alipay/alex-core';
77
import { CompletionItemKind, SqlServiceModule, supportLanguage, setMonacoEnvironment } from '@alipay/alex-sql-service';
8-
import dtSql from '@alipay/alex/extensions/alex.dt-sql';
9-
import css from '@alipay/alex/extensions/alex-ext-public.css-language-features-worker';
10-
import html from '@alipay/alex/extensions/alex-ext-public.html-language-features-worker';
11-
import json from '@alipay/alex/extensions/alex-ext-public.json-language-features-worker';
12-
import markdown from '@alipay/alex/extensions/alex-ext-public.markdown-language-features-worker';
13-
import typescript from '@alipay/alex/extensions/alex-ext-public.typescript-language-features-worker';
8+
// import dtSql from '@alipay/alex/extensions/alex.dt-sql';
9+
// import css from '@alipay/alex/extensions/alex-ext-public.css-language-features-worker';
10+
// import html from '@alipay/alex/extensions/alex-ext-public.html-language-features-worker';
11+
// import json from '@alipay/alex/extensions/alex-ext-public.json-language-features-worker';
12+
// import markdown from '@alipay/alex/extensions/alex-ext-public.markdown-language-features-worker';
13+
// import typescript from '@alipay/alex/extensions/alex-ext-public.typescript-language-features-worker';
1414

1515
(window as any).alex = Alex;
1616
setMonacoEnvironment()
@@ -30,8 +30,6 @@ const layoutConfig = {
3030
},
3131
};
3232

33-
const defaultContent = `SELECT * FROM table`;
34-
3533
const App = () => (
3634
<AppRenderer
3735
onLoad={(app) => {
@@ -42,7 +40,7 @@ const App = () => (
4240
modules: [
4341
SqlServiceModule.Config({
4442
onValidation: (ast: any, markers: any) => {
45-
console.log(ast, markers);
43+
// console.log(ast, markers);
4644
// const filterMarkers = markers.filter((item, index) => {
4745
// if (item.message == 'SEMICOLON expected.') {
4846
// const markerText = editorIns.current.editor.getModel().getValueInRange({
@@ -56,9 +54,21 @@ const App = () => (
5654
// }
5755
// }
5856
// });
59-
// return filterMarkers;
60-
return [];
57+
return markers;
6158
},
59+
lowerCaseComplete: true,
60+
marks:[
61+
{
62+
message: "测试效果",
63+
startRow: 1,
64+
startCol: 1
65+
}
66+
],
67+
// @ts-ignore
68+
formatRules: [{
69+
// regex: /\w/g,
70+
// value: 'select',
71+
}],
6272
onSuggestTables: (keyword, options) => {
6373
console.log(keyword, options);
6474
return Promise.resolve([
@@ -71,32 +81,98 @@ const App = () => (
7181
},
7282
]);
7383
},
84+
onSuggestFields: (prefix, options) =>{
85+
return Promise.resolve([
86+
{
87+
label: "age",
88+
type: "SAMPLE_TYPE_ONE",
89+
insertText: "age",
90+
kind: CompletionItemKind.Field,
91+
sortText: "b"
92+
},
93+
{
94+
label: "banana",
95+
type: "SAMPLE_TYPE_ONE",
96+
insertText: "banana",
97+
kind: CompletionItemKind.Field,
98+
sortText: "b"
99+
},
100+
{
101+
label: "sample_one_table1",
102+
type: "SAMPLE_TYPE_ONE",
103+
insertText: "id_test",
104+
kind: CompletionItemKind.Field,
105+
sortText: "b"
106+
}
107+
])
108+
},
74109
options: {
75-
value: defaultContent,
110+
// value: defaultContent,
76111
language: supportLanguage.ODPSSQL,
77112
},
78-
onChange: (value) => {
79-
console.log(value);
113+
sorter: (type) => {
114+
switch (type) {
115+
case 'TABLEALIAS':
116+
case 'TABLE':
117+
return 'c';
118+
case 'FIELD':
119+
case 'FIELDALIAS':
120+
return 'd';
121+
case 'KEYWORD':
122+
case 'CONSTS':
123+
return 'e';
124+
case 'FUNCTION':
125+
return 'f';
126+
default:
127+
return 'g'
128+
}
80129
},
81-
// completionTypes: {
82-
// KEYWORD: {
83-
// text: '关键词',
84-
// kind: CompletionItemKind.Keyword,
85-
// },
86-
// CUSTOM: {
87-
// text: '手动配置关键词',
88-
// kind: CompletionItemKind.Keyword,
89-
// },
130+
// onChange: (value) => {
131+
// console.log(value);
90132
// },
133+
completionTypes: {
134+
KEYWORD: {
135+
text: "关键词",
136+
kind: CompletionItemKind.Keyword
137+
},
138+
CONSTS: {
139+
text: "常量",
140+
kind: CompletionItemKind.Snippet
141+
},
142+
FUNCTION: {
143+
text: "函数",
144+
kind: CompletionItemKind.Function
145+
},
146+
TABLE: {
147+
text: "表名",
148+
kind: CompletionItemKind.Method
149+
},
150+
TABLEALIAS: {
151+
text: "表别名",
152+
kind: CompletionItemKind.Method
153+
},
154+
SNIPPET: {
155+
text: "代码块",
156+
kind: CompletionItemKind.Snippet
157+
},
158+
FIELD: {
159+
text: "表字段",
160+
kind: CompletionItemKind.Field
161+
},
162+
FIELDALIAS: {
163+
text: "表字段别名",
164+
kind: CompletionItemKind.Field
165+
}
166+
},
91167
}),
92168
],
93169
extensionMetadata: [
94170
// dtSql,
95-
css,
96-
html,
97-
json,
98-
markdown,
99-
typescript
171+
// css,
172+
// html,
173+
// json,
174+
// markdown,
175+
// typescript
100176

101177
],
102178
workspaceDir: `sql-service`,
@@ -121,7 +197,7 @@ const App = () => (
121197
'main.html': '<div id="root"></div>',
122198
'main.css': 'body {}',
123199
'main.js': 'console.log("main")',
124-
'test.sql': 'SELECT * FROM table',
200+
'test.sql': 'select * from',
125201
});
126202
},
127203
},

packages/sql-service/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"registry": "https://registry.npm.alibaba-inc.com"
1616
},
1717
"dependencies": {
18-
"chevrotain": "^7.0.0"
18+
"chevrotain": "^7.0.0",
19+
"bravo-parser": "1.0.64",
20+
"sql-prettier": "^0.2.12"
1921
},
2022
"tnpm": {
2123
"mode": "yarn",

packages/sql-service/src/contribution/sql-service.contribution.ts

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { ISQLServiceConfig } from './sql-service.configuration';
2222
import {
2323
CustomCompletionProviderOptions,
2424
CustomEditorInstance,
25+
languageGrammer,
2526
languageRegister,
2627
supportLanguage,
2728
} from '../types';
@@ -46,7 +47,6 @@ export class SqlServiceContribution implements ClientAppContribution {
4647
// }
4748

4849
onDidStart(app: IClientApp): MaybePromise<void> {
49-
console.log(this.sqlConfig);
5050
this.registerLanguage(supportLanguage.ODPSSQL);
5151
const editorMap = new Map<string, CustomEditorInstance>();
5252

@@ -90,6 +90,7 @@ export class SqlServiceContribution implements ClientAppContribution {
9090
// 仅占位
9191
const uri = new URI();
9292
this.textmateService.registerLanguages([languageRegister[languageId]], uri);
93+
this.textmateService.registerGrammar(languageGrammer[languageId], uri);
9394
}
9495

9596
setUp(
@@ -111,50 +112,45 @@ export class SqlServiceContribution implements ClientAppContribution {
111112
}
112113

113114
let CompletionAdapter = new SQLGenericsFeatures.CompletionAdapter(worker, options, languageId);
114-
// let DiagnosticsAdapter = new SQLGenericsFeatures.DiagnosticsAdapter(
115-
// languageId,
116-
// worker,
117-
// defaults,
118-
// options
119-
// );
120-
console.log(' ==> CompletionAdapter', CompletionAdapter, languageId);
115+
let DiagnosticsAdapter = new SQLGenericsFeatures.DiagnosticsAdapter(
116+
languageId,
117+
worker,
118+
defaults,
119+
options
120+
);
121121
monaco.languages.registerCompletionItemProvider(languageId, CompletionAdapter);
122-
// redis,odps,hive开启hover功能
123122

124-
// monaco.languages.registerHoverProvider(
125-
// languageId,
126-
// new SQLGenericsFeatures.HoverAdapter(worker, options)
127-
// );
123+
monaco.languages.registerHoverProvider(
124+
languageId,
125+
new SQLGenericsFeatures.HoverAdapter(worker, options)
126+
);
128127

129-
// monaco.languages.registerDocumentFormattingEditProvider(
130-
// languageId,
131-
// new SQLGenericsFeatures.DocumentFormattingEditAdapter(worker, options)
132-
// );
128+
monaco.languages.registerDocumentFormattingEditProvider(
129+
languageId,
130+
new SQLGenericsFeatures.DocumentFormattingEditAdapter(worker, options)
131+
);
133132

134-
// // 目前就支持ODPS,其余到时看需求扩展,需要对应work增加内容
135-
// monaco.languages.registerDefinitionProvider(
136-
// languageId,
137-
// new SQLGenericsFeatures.DefinitionAdapter(worker, options)
138-
// );
139-
// // autofix 仅支持ODPS先
140-
// monaco.languages.registerCodeActionProvider(
141-
// languageId,
142-
// new SQLGenericsFeatures.CodeActionProvider(worker, options)
143-
// );
133+
// 目前就支持ODPS,其余到时看需求扩展,需要对应work增加内容
134+
monaco.languages.registerDefinitionProvider(
135+
languageId,
136+
new SQLGenericsFeatures.DefinitionAdapter(worker, options)
137+
);
138+
// autofix 仅支持ODPS先
139+
monaco.languages.registerCodeActionProvider(
140+
languageId,
141+
new SQLGenericsFeatures.CodeActionProvider(worker, options)
142+
);
144143

145-
// // ODPS 注册函数参数提示
146-
// monaco.languages.registerSignatureHelpProvider(
147-
// languageId,
148-
// new SQLGenericsFeatures.SignatureHelpAdapter(worker, options)
149-
// );
144+
// ODPS 注册函数参数提示
145+
monaco.languages.registerSignatureHelpProvider(
146+
languageId,
147+
new SQLGenericsFeatures.SignatureHelpAdapter(worker, options)
148+
);
150149

151150
// /** 选中内容格式化 */
152151
// monaco.languages.registerDocumentRangeFormattingEditProvider(
153152
// languageId,
154153
// new SQLGenericsFeatures.DocumentRangeFormattingEditAdapter(worker, options)
155154
// );
156155
}
157-
158156
}
159-
160-

packages/sql-service/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function generateCorsUrl(url) {
3737
export function setMonacoEnvironment() {
3838
self['MonacoEnvironment'] = {
3939
getWorkerUrl: function () {
40-
return generateCorsUrl('https://g.alicdn.com/nel/bravo-editor/0.0.103/odps.worker.js');
40+
return generateCorsUrl('http://127.0.0.1:8080/webpack/dist/worker/sql-odps.worker.js');
4141
},
4242
};
4343
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { WorkerAccessor, CompletionProviderOptions } from '../types';
2+
import { Range } from '@opensumi/monaco-editor-core/esm/vs/editor/editor.api';
3+
import * as monaco from '@opensumi/monaco-editor-core/esm/vs/editor/editor.api'
4+
class CodeActionProvider implements monaco.languages.CodeActionProvider {
5+
private options: CompletionProviderOptions;
6+
7+
constructor(private _worker: WorkerAccessor, options: CompletionProviderOptions) {
8+
this.options = options;
9+
}
10+
11+
async provideCodeActions(
12+
model: monaco.editor.ITextModel,
13+
range: Range,
14+
context: monaco.languages.CodeActionContext,
15+
token,
16+
...rest
17+
): Promise<monaco.languages.CodeActionList> {
18+
const { onQuickfix } = this.options;
19+
if (onQuickfix) {
20+
// https://stackoverflow.com/questions/57994101/show-quick-fix-for-an-error-in-monaco-editor
21+
const markers = await onQuickfix(context.markers);
22+
let actions = markers.map(({ title, replace, range, error, jumpUrl }, index) => {
23+
const diagnostic = error || context.markers[index];
24+
25+
const common = {
26+
title,
27+
diagnostics: [diagnostic],
28+
kind: 'quickfix',
29+
isPreferred: true,
30+
};
31+
32+
// if (jumpUrl) {
33+
// const [url, cacheData] = jumpUrl.split('&tableCacheInLocal=');
34+
// return {
35+
// ...common,
36+
// command: {
37+
// title: '跳转链接',
38+
// id: 'jumpTableApply',
39+
// arguments: [url, cacheData],
40+
// },
41+
// };
42+
// }
43+
44+
return {
45+
...common,
46+
edit: {
47+
edits: [
48+
{
49+
resource: model.uri,
50+
textEdit: {
51+
range: range || { ...diagnostic },
52+
text: replace, // text to replace with
53+
},
54+
},
55+
],
56+
},
57+
} as monaco.languages.CodeAction;
58+
});
59+
60+
const list = {
61+
actions,
62+
dispose: () => {}
63+
} as monaco.languages.CodeActionList;
64+
// @ts-ignore
65+
return Promise.resolve(list)
66+
}
67+
return Promise.resolve({
68+
actions: [],
69+
dispose: () =>{}
70+
} as unknown as monaco.languages.CodeActionList);
71+
}
72+
}
73+
74+
export default CodeActionProvider;

0 commit comments

Comments
 (0)