@difizen/libro-cofine-editor 0.1.13 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@difizen/libro-cofine-editor",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "libro",
@@ -32,13 +32,13 @@
32
32
  "src"
33
33
  ],
34
34
  "dependencies": {
35
- "@difizen/monaco-editor-core": "latest",
36
- "@difizen/libro-cofine-editor-core": "^0.1.13",
37
- "@difizen/libro-cofine-textmate": "^0.1.13",
38
- "@difizen/libro-lsp": "^0.1.13",
39
- "@difizen/libro-code-editor": "^0.1.13",
40
- "@difizen/libro-common": "^0.1.13",
41
- "@difizen/libro-core": "^0.1.13",
35
+ "@difizen/monaco-editor-core": "0.39.4",
36
+ "@difizen/libro-cofine-editor-core": "^0.1.15",
37
+ "@difizen/libro-cofine-textmate": "^0.1.15",
38
+ "@difizen/libro-lsp": "^0.1.15",
39
+ "@difizen/libro-code-editor": "^0.1.15",
40
+ "@difizen/libro-common": "^0.1.15",
41
+ "@difizen/libro-core": "^0.1.15",
42
42
  "@difizen/mana-app": "latest",
43
43
  "resize-observer-polyfill": "^1.5.1",
44
44
  "vscode-languageserver-protocol": "^3.17.4",
@@ -1,11 +1,19 @@
1
- import type { CodeEditorFactory } from '@difizen/libro-code-editor';
1
+ import type { CodeEditorFactory, EditorStateFactory } from '@difizen/libro-code-editor';
2
2
  import { CodeEditorContribution } from '@difizen/libro-code-editor';
3
3
  import { inject, singleton } from '@difizen/mana-app';
4
4
 
5
- import { libroE2DefaultConfig, LibroE2EditorFactory } from './libro-e2-editor.js';
5
+ import { LanguageSpecRegistry } from './language-specs.js';
6
+ import {
7
+ e2StateFactory,
8
+ libroE2DefaultConfig,
9
+ LibroE2EditorFactory,
10
+ } from './libro-e2-editor.js';
6
11
 
7
12
  @singleton({ contrib: [CodeEditorContribution] })
8
13
  export class LibroE2EditorContribution implements CodeEditorContribution {
14
+ @inject(LanguageSpecRegistry)
15
+ protected readonly languageSpecRegistry: LanguageSpecRegistry;
16
+
9
17
  factory: CodeEditorFactory;
10
18
 
11
19
  defaultConfig = libroE2DefaultConfig;
@@ -16,6 +24,13 @@ export class LibroE2EditorContribution implements CodeEditorContribution {
16
24
  this.factory = libroE2EditorFactory;
17
25
  }
18
26
 
27
+ stateFactory: EditorStateFactory<any> = (options) => {
28
+ return e2StateFactory(this.languageSpecRegistry)({
29
+ uuid: options.uuid,
30
+ model: options.model,
31
+ });
32
+ };
33
+
19
34
  canHandle(mime: string): number {
20
35
  const mimes = [
21
36
  'application/vnd.libro.sql+json',
@@ -140,10 +140,9 @@ export class CompletionProvider
140
140
  if (!original) {
141
141
  return;
142
142
  }
143
+ const lspConnection = await this.getLSPConnection();
143
144
  const itemResult =
144
- await this.lspConnection.clientRequests['completionItem/resolve'].request(
145
- original,
146
- );
145
+ await lspConnection.clientRequests['completionItem/resolve'].request(original);
147
146
  if (token.isCancellationRequested) {
148
147
  return;
149
148
  }
@@ -1,6 +1,6 @@
1
1
  import type { LibroService } from '@difizen/libro-core';
2
2
  import { EditorCellView } from '@difizen/libro-core';
3
- import type { LSPConnection, VirtualDocument } from '@difizen/libro-lsp';
3
+ import type { ILSPDocumentConnectionManager } from '@difizen/libro-lsp';
4
4
  import { DisposableCollection } from '@difizen/mana-app';
5
5
  import type { Disposable } from '@difizen/mana-app';
6
6
  import * as monaco from '@difizen/monaco-editor-core';
@@ -28,10 +28,9 @@ export class DiagnosticProvider extends LangaugeFeatureProvider implements Dispo
28
28
  protected toDispose = new DisposableCollection();
29
29
  constructor(
30
30
  libroService: LibroService,
31
- lspConnection: LSPConnection,
32
- virtualDocument: VirtualDocument,
31
+ lspDocumentConnectionManager: ILSPDocumentConnectionManager,
33
32
  ) {
34
- super(libroService, lspConnection, virtualDocument);
33
+ super(libroService, lspDocumentConnectionManager);
35
34
  this.processDiagnostic();
36
35
  }
37
36
 
@@ -79,71 +78,79 @@ export class DiagnosticProvider extends LangaugeFeatureProvider implements Dispo
79
78
  }
80
79
 
81
80
  async processDiagnostic() {
82
- const toDispose = this.lspConnection.serverNotifications[
81
+ const lspConnection = await this.getLSPConnection();
82
+ const toDispose = lspConnection.serverNotifications[
83
83
  'textDocument/publishDiagnostics'
84
- ].event((e) => {
84
+ ].event(async (e) => {
85
85
  this.diagnosticList = [];
86
- e.diagnostics.forEach((diagnostic) => {
87
- const { range } = diagnostic;
88
- // the diagnostic range must be in current editor
89
- const editor = this.getEditorFromLSPPosition(range);
90
- if (!editor || editor.getOption('lspEnabled') !== true) {
91
- return;
92
- }
93
- const model = editor?.monacoEditor?.getModel();
94
- if (!model) {
95
- return;
96
- }
86
+ await Promise.all(
87
+ e.diagnostics.map(async (diagnostic) => {
88
+ const { range } = diagnostic;
89
+ // the diagnostic range must be in current editor
90
+ const editor = await this.getEditorFromLSPPosition(range);
91
+ if (!editor || editor.getOption('lspEnabled') !== true) {
92
+ return;
93
+ }
94
+ const model = editor?.monacoEditor?.getModel();
95
+ if (!model) {
96
+ return;
97
+ }
97
98
 
98
- const editorStart = this.virtualDocument.transformVirtualToEditor({
99
- line: range.start.line,
100
- ch: range.start.character,
101
- isVirtual: true,
102
- });
99
+ const virtualDocument = await this.getVirtualDocument();
100
+ if (!virtualDocument) {
101
+ return;
102
+ }
103
103
 
104
- const editorEnd = this.virtualDocument.transformVirtualToEditor({
105
- line: range.end.line,
106
- ch: range.end.character,
107
- isVirtual: true,
108
- });
104
+ const editorStart = virtualDocument.transformVirtualToEditor({
105
+ line: range.start.line,
106
+ ch: range.start.character,
107
+ isVirtual: true,
108
+ });
109
109
 
110
- if (!editorStart || !editorEnd) {
111
- return;
112
- }
110
+ const editorEnd = virtualDocument.transformVirtualToEditor({
111
+ line: range.end.line,
112
+ ch: range.end.character,
113
+ isVirtual: true,
114
+ });
113
115
 
114
- const markerRange = new MonacoRange(
115
- editorStart.line + 1,
116
- editorStart.ch,
117
- editorEnd.line + 1,
118
- editorEnd.ch,
119
- );
120
-
121
- const marker: monaco.editor.IMarkerData = {
122
- source: diagnostic.source,
123
- tags: diagnostic.tags,
124
- message: diagnostic.message,
125
- code: String(diagnostic.code),
126
- severity: diagnostic.severity
127
- ? vererityMap[diagnostic.severity]
128
- : monaco.MarkerSeverity.Info,
129
- relatedInformation: diagnostic.relatedInformation?.map((item) => {
130
- return {
131
- message: item.message,
132
- resource: MonacoUri.parse(item.location.uri),
133
- startLineNumber: markerRange.startLineNumber,
134
- startColumn: markerRange.startColumn,
135
- endLineNumber: markerRange.endLineNumber,
136
- endColumn: markerRange.endColumn,
137
- };
138
- }),
139
- startLineNumber: editorStart.line + 1,
140
- startColumn: editorStart.ch + 1,
141
- endLineNumber: editorEnd.line + 1,
142
- endColumn: editorEnd.ch + 1,
143
- };
144
-
145
- this.addDiagnostic(model, marker);
146
- });
116
+ if (!editorStart || !editorEnd) {
117
+ return;
118
+ }
119
+
120
+ const markerRange = new MonacoRange(
121
+ editorStart.line + 1,
122
+ editorStart.ch,
123
+ editorEnd.line + 1,
124
+ editorEnd.ch,
125
+ );
126
+
127
+ const marker: monaco.editor.IMarkerData = {
128
+ source: diagnostic.source,
129
+ tags: diagnostic.tags,
130
+ message: diagnostic.message,
131
+ code: String(diagnostic.code),
132
+ severity: diagnostic.severity
133
+ ? vererityMap[diagnostic.severity]
134
+ : monaco.MarkerSeverity.Info,
135
+ relatedInformation: diagnostic.relatedInformation?.map((item) => {
136
+ return {
137
+ message: item.message,
138
+ resource: MonacoUri.parse(item.location.uri),
139
+ startLineNumber: markerRange.startLineNumber,
140
+ startColumn: markerRange.startColumn,
141
+ endLineNumber: markerRange.endLineNumber,
142
+ endColumn: markerRange.endColumn,
143
+ };
144
+ }),
145
+ startLineNumber: editorStart.line + 1,
146
+ startColumn: editorStart.ch + 1,
147
+ endLineNumber: editorEnd.line + 1,
148
+ endColumn: editorEnd.ch + 1,
149
+ };
150
+
151
+ this.addDiagnostic(model, marker);
152
+ }),
153
+ );
147
154
 
148
155
  this.displayDiagnostic();
149
156
  });
@@ -38,7 +38,8 @@ export class FormatProvider
38
38
  }
39
39
 
40
40
  const { virtualDocument: doc } = provider;
41
- const result = await this.lspConnection.clientRequests[
41
+ const lspConnection = await this.getLSPConnection();
42
+ const result = await lspConnection.clientRequests[
42
43
  'textDocument/rangeFormatting'
43
44
  ].request({
44
45
  // TODO: range transform
@@ -1,7 +1,7 @@
1
1
  import type { LibroService } from '@difizen/libro-core';
2
2
  import { EditorCellView } from '@difizen/libro-core';
3
- import type { LSPProviderResult } from '@difizen/libro-lsp';
4
- import type { LSPConnection, VirtualDocument } from '@difizen/libro-lsp';
3
+ import type { ILSPDocumentConnectionManager } from '@difizen/libro-lsp';
4
+ import type { LSPConnection } from '@difizen/libro-lsp';
5
5
  import type monaco from '@difizen/monaco-editor-core';
6
6
  import type * as lsp from 'vscode-languageserver-protocol';
7
7
 
@@ -9,19 +9,52 @@ import { LibroE2Editor } from '../../libro-e2-editor.js';
9
9
 
10
10
  export class LangaugeFeatureProvider {
11
11
  protected libroService: LibroService;
12
- protected lspProvider?: LSPProviderResult;
13
- protected lspConnection: LSPConnection;
14
- virtualDocument: VirtualDocument;
12
+ lspDocumentConnectionManager: ILSPDocumentConnectionManager;
15
13
  constructor(
16
14
  libroService: LibroService,
17
- lspConnection: LSPConnection,
18
- virtualDocument: VirtualDocument,
15
+ lspDocumentConnectionManager: ILSPDocumentConnectionManager,
19
16
  ) {
20
17
  this.libroService = libroService;
21
- this.lspConnection = lspConnection;
22
- this.virtualDocument = virtualDocument;
18
+ this.lspDocumentConnectionManager = lspDocumentConnectionManager;
23
19
  }
24
20
 
21
+ async getVirtualDocument() {
22
+ const libroView = this.libroService.active;
23
+ if (!libroView) {
24
+ return;
25
+ }
26
+ await this.lspDocumentConnectionManager.ready;
27
+ const adapter = this.lspDocumentConnectionManager.adapters.get(libroView.model.id);
28
+ if (!adapter) {
29
+ throw new Error('no adapter');
30
+ }
31
+
32
+ await adapter.ready;
33
+
34
+ // Get the associated virtual document of the opened document
35
+ const virtualDocument = adapter.virtualDocument;
36
+ return virtualDocument;
37
+ }
38
+
39
+ async getLSPConnection() {
40
+ const virtualDocument = await this.getVirtualDocument();
41
+ if (!virtualDocument) {
42
+ throw new Error('no virtualDocument');
43
+ }
44
+
45
+ // Get the LSP connection of the virtual document.
46
+ const lspConnection = this.lspDocumentConnectionManager.connections.get(
47
+ virtualDocument.uri,
48
+ ) as LSPConnection;
49
+
50
+ return lspConnection;
51
+ }
52
+
53
+ /**
54
+ * find cell editor from active notebook by model uri
55
+ * @param model
56
+ * @returns
57
+ */
25
58
  protected getEditorByModel(model: monaco.editor.ITextModel) {
26
59
  const cells = this.libroService.active?.model.cells;
27
60
  if (!cells) {
@@ -42,17 +75,23 @@ export class LangaugeFeatureProvider {
42
75
  return (cell as EditorCellView).editor as LibroE2Editor | undefined;
43
76
  }
44
77
 
45
- protected getEditorFromLSPPosition(range: lsp.Range) {
46
- const currentEditor = this.virtualDocument.getEditorAtVirtualLine({
47
- line: range.start.line,
48
- ch: range.start.character,
49
- isVirtual: true,
50
- });
51
- const editor = currentEditor.getEditor();
52
- if (editor instanceof LibroE2Editor) {
53
- return editor;
78
+ protected async getEditorFromLSPPosition(range: lsp.Range) {
79
+ try {
80
+ const virtualDocument = await this.getVirtualDocument();
81
+ const currentEditor = virtualDocument?.getEditorAtVirtualLine({
82
+ line: range.start.line,
83
+ ch: range.start.character,
84
+ isVirtual: true,
85
+ });
86
+ const editor = currentEditor?.getEditor();
87
+ if (editor instanceof LibroE2Editor) {
88
+ return editor;
89
+ }
90
+ return;
91
+ } catch (error) {
92
+ console.warn(error);
93
+ return;
54
94
  }
55
- return;
56
95
  }
57
96
 
58
97
  protected async getProvider(model: monaco.editor.ITextModel) {
@@ -63,7 +102,6 @@ export class LangaugeFeatureProvider {
63
102
  }
64
103
 
65
104
  const provider = await editor.lspProvider?.();
66
- this.lspProvider = provider;
67
105
  return provider;
68
106
  }
69
107
  }
@@ -1,12 +1,12 @@
1
- import {
2
- EditorHandlerContribution,
3
- LanguageOptionsRegistry,
4
- } from '@difizen/libro-cofine-editor-core';
1
+ import { EditorHandlerContribution } from '@difizen/libro-cofine-editor-core';
5
2
  import { LibroService } from '@difizen/libro-core';
6
- import type { LSPConnection } from '@difizen/libro-lsp';
7
3
  import { ILSPDocumentConnectionManager } from '@difizen/libro-lsp';
8
4
  import { Disposable, DisposableCollection, inject, singleton } from '@difizen/mana-app';
9
- import * as monaco from '@difizen/monaco-editor-core';
5
+ import type { editor } from '@difizen/monaco-editor-core';
6
+ import { languages } from '@difizen/monaco-editor-core';
7
+ // import { ILanguageFeaturesService } from '@difizen/monaco-editor-core/esm/vs/editor/common/services/languageFeatures.js';
8
+ // import { StandaloneServices } from '@difizen/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices.js';
9
+ // import { getSingletonServiceDescriptors } from '@difizen/monaco-editor-core/esm/vs/platform/instantiation/common/extensions.js';
10
10
 
11
11
  import { LibroE2URIScheme } from '../../libro-e2-editor.js';
12
12
 
@@ -19,9 +19,6 @@ import { SignatureHelpProvider } from './signature-help-provider.js';
19
19
  contrib: [EditorHandlerContribution],
20
20
  })
21
21
  export class LSPContribution implements EditorHandlerContribution {
22
- @inject(LanguageOptionsRegistry)
23
- protected readonly optionsResgistry: LanguageOptionsRegistry;
24
-
25
22
  @inject(LibroService) protected readonly libroService: LibroService;
26
23
 
27
24
  @inject(ILSPDocumentConnectionManager)
@@ -35,117 +32,84 @@ export class LSPContribution implements EditorHandlerContribution {
35
32
  //
36
33
  }
37
34
  afterCreate(editor: any) {
38
- this.registerLSPFeature(editor as monaco.editor.IStandaloneCodeEditor);
35
+ this.registerLSPFeature(editor as editor.IStandaloneCodeEditor);
36
+ // const languageFeaturesService = getOrigin(StandaloneServices).get(
37
+ // ILanguageFeaturesService,
38
+ // );
39
+ // for (const [id, descriptor] of getSingletonServiceDescriptors()) {
40
+ // if (id === ILanguageFeaturesService) {
41
+ // console.log('lll', descriptor);
42
+ // }
43
+ // }
44
+ // console.log(
45
+ // StandaloneServices,
46
+ // StandaloneServices.initialize(),
47
+ // getOrigin(languageFeaturesService),
48
+ // );
39
49
  }
40
50
  canHandle(language: string) {
41
51
  return this.lspLangs.includes(language);
42
52
  }
43
53
 
44
- getLanguageSelector(
45
- model: monaco.editor.ITextModel,
46
- ): monaco.languages.LanguageFilter {
54
+ getLanguageSelector(model: editor.ITextModel): languages.LanguageFilter {
47
55
  return {
48
56
  scheme: LibroE2URIScheme,
49
57
  pattern: model.uri.path,
50
58
  };
51
59
  }
52
60
 
53
- async getVirtualDocument() {
54
- const libroView = this.libroService.active;
55
- if (!libroView) {
56
- return;
57
- }
58
- await this.lspDocumentConnectionManager.ready;
59
- const adapter = this.lspDocumentConnectionManager.adapters.get(libroView.model.id);
60
- if (!adapter) {
61
- throw new Error('no adapter');
62
- }
63
-
64
- await adapter.ready;
65
-
66
- // Get the associated virtual document of the opened document
67
- const virtualDocument = adapter.virtualDocument;
68
- return virtualDocument;
69
- }
70
-
71
- async getLSPConnection() {
72
- const virtualDocument = await this.getVirtualDocument();
73
- if (!virtualDocument) {
74
- throw new Error('no virtualDocument');
75
- }
76
-
77
- // Get the LSP connection of the virtual document.
78
- const lspConnection = this.lspDocumentConnectionManager.connections.get(
79
- virtualDocument.uri,
80
- ) as LSPConnection;
81
-
82
- return lspConnection;
83
- }
84
-
85
- registerLSPFeature(editor: monaco.editor.IStandaloneCodeEditor) {
61
+ registerLSPFeature(editor: editor.IStandaloneCodeEditor) {
86
62
  const model = editor.getModel();
87
63
  if (!model) {
88
64
  return;
89
65
  }
90
66
 
91
- Promise.all([this.getVirtualDocument(), this.getLSPConnection()])
92
- .then(([virtualDocument, lspConnection]) => {
93
- if (!lspConnection || !virtualDocument) {
94
- return;
95
- }
96
- this.toDispose.push(
97
- monaco.languages.registerCompletionItemProvider(
98
- this.getLanguageSelector(model),
99
- new CompletionProvider(this.libroService, lspConnection, virtualDocument),
100
- ),
101
- );
102
-
103
- this.toDispose.push(
104
- monaco.languages.registerHoverProvider(
105
- this.getLanguageSelector(model),
106
- new HoverProvider(this.libroService, lspConnection, virtualDocument),
107
- ),
108
- );
109
-
110
- const provider = new DiagnosticProvider(
111
- this.libroService,
112
- lspConnection,
113
- virtualDocument,
114
- );
115
- this.toDispose.push(Disposable.create(() => provider.dispose()));
116
-
117
- this.toDispose.push(
118
- monaco.languages.registerSignatureHelpProvider(
119
- this.getLanguageSelector(model),
120
- new SignatureHelpProvider(
121
- this.libroService,
122
- lspConnection,
123
- virtualDocument,
124
- ),
125
- ),
126
- );
127
- // const formatProvider = new FormatProvider(
128
- // this.libroService,
129
- // lspConnection,
130
- // virtualDocument,
131
- // );
132
- // monaco.languages.registerDocumentFormattingEditProvider(
133
- // this.getLanguageSelector(model),
134
- // formatProvider,
135
- // );
136
- // monaco.languages.registerDocumentRangeFormattingEditProvider(
137
- // this.getLanguageSelector(model),
138
- // formatProvider,
139
- // );
140
- return;
141
- })
142
- .catch(console.error);
67
+ this.toDispose.push(
68
+ languages.registerCompletionItemProvider(
69
+ this.getLanguageSelector(model),
70
+ new CompletionProvider(this.libroService, this.lspDocumentConnectionManager),
71
+ ),
72
+ );
73
+
74
+ this.toDispose.push(
75
+ languages.registerHoverProvider(
76
+ this.getLanguageSelector(model),
77
+ new HoverProvider(this.libroService, this.lspDocumentConnectionManager),
78
+ ),
79
+ );
80
+
81
+ const provider = new DiagnosticProvider(
82
+ this.libroService,
83
+ this.lspDocumentConnectionManager,
84
+ );
85
+ this.toDispose.push(Disposable.create(() => provider.dispose()));
86
+
87
+ this.toDispose.push(
88
+ languages.registerSignatureHelpProvider(
89
+ this.getLanguageSelector(model),
90
+ new SignatureHelpProvider(this.libroService, this.lspDocumentConnectionManager),
91
+ ),
92
+ );
93
+ // const formatProvider = new FormatProvider(
94
+ // this.libroService,
95
+ // lspConnection,
96
+ // virtualDocument,
97
+ // );
98
+ // languages.registerDocumentFormattingEditProvider(
99
+ // this.getLanguageSelector(model),
100
+ // formatProvider,
101
+ // );
102
+ // languages.registerDocumentRangeFormattingEditProvider(
103
+ // this.getLanguageSelector(model),
104
+ // formatProvider,
105
+ // );
106
+ return;
143
107
 
144
108
  // // SignatureHelp
145
- // monaco.languages.registerSignatureHelpProvider(id, new SignatureHelpProvider(this._worker));
109
+ // languages.registerSignatureHelpProvider(id, new SignatureHelpProvider(this._worker));
146
110
 
147
111
  // // 定义跳转;
148
- // monaco.languages.registerDefinitionProvider(id, new DefinitionAdapter(this._worker));
112
+ // languages.registerDefinitionProvider(id, new DefinitionAdapter(this._worker));
149
113
  }
150
114
 
151
115
  protected isDisposed = false;