@jupyterlab/lsp 4.0.0-alpha.19 → 4.0.0-alpha.21

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.
@@ -0,0 +1,84 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { ILSPCodeExtractorsManager } from '../tokens';
5
+ import { IForeignCodeExtractor } from './types';
6
+
7
+ /**
8
+ * Manager for the code extractors
9
+ */
10
+ export class CodeExtractorsManager implements ILSPCodeExtractorsManager {
11
+ constructor() {
12
+ this._extractorMap = new Map<
13
+ string,
14
+ Map<string, IForeignCodeExtractor[]>
15
+ >();
16
+
17
+ this._extractorMapAnyLanguage = new Map<string, IForeignCodeExtractor[]>();
18
+ }
19
+
20
+ /**
21
+ * Get the extractors for the input cell type and the main language of
22
+ * the document
23
+ *
24
+ * @param cellType - type of cell
25
+ * @param hostLanguage - main language of the document
26
+ */
27
+ getExtractors(
28
+ cellType: string,
29
+ hostLanguage: string | null
30
+ ): IForeignCodeExtractor[] {
31
+ if (hostLanguage) {
32
+ const currentMap = this._extractorMap.get(cellType);
33
+ if (!currentMap) {
34
+ return [];
35
+ }
36
+ return currentMap!.get(hostLanguage) ?? [];
37
+ } else {
38
+ return this._extractorMapAnyLanguage.get(cellType) ?? [];
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Register an extractor to extract foreign code from host documents of specified language.
44
+ */
45
+ register(
46
+ extractor: IForeignCodeExtractor,
47
+ hostLanguage: string | null
48
+ ): void {
49
+ const cellType = extractor.cellType;
50
+ if (hostLanguage) {
51
+ cellType.forEach(type => {
52
+ if (!this._extractorMap.has(type)) {
53
+ this._extractorMap.set(type, new Map());
54
+ }
55
+ const currentMap = this._extractorMap.get(type)!;
56
+ const extractorList = currentMap.get(hostLanguage);
57
+ if (!extractorList) {
58
+ currentMap.set(hostLanguage, [extractor]);
59
+ } else {
60
+ extractorList.push(extractor);
61
+ }
62
+ });
63
+ } else {
64
+ cellType.forEach(type => {
65
+ if (!this._extractorMapAnyLanguage.has(type)) {
66
+ this._extractorMapAnyLanguage.set(type, []);
67
+ }
68
+ this._extractorMapAnyLanguage.get(type)!.push(extractor);
69
+ });
70
+ }
71
+ }
72
+
73
+ /**
74
+ * The map with key is the type of cell, value is another map between
75
+ * the language of cell and its code extractor.
76
+ */
77
+ private _extractorMap: Map<string, Map<string, IForeignCodeExtractor[]>>;
78
+
79
+ /**
80
+ * The map with key is the cell type, value is the code extractor associated
81
+ * with this cell type, this is used for the non-code cell types.
82
+ */
83
+ private _extractorMapAnyLanguage: Map<string, IForeignCodeExtractor[]>;
84
+ }
@@ -0,0 +1,94 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { LanguageIdentifier } from '../lsp';
5
+ import { positionAtOffset } from '../positioning';
6
+
7
+ import { IExtractedCode, IForeignCodeExtractor } from './types';
8
+
9
+ /**
10
+ * The code extractor for the raw and markdown text.
11
+ */
12
+ export class TextForeignCodeExtractor implements IForeignCodeExtractor {
13
+ constructor(options: TextForeignCodeExtractor.IOptions) {
14
+ this.language = options.language;
15
+ this.standalone = options.isStandalone;
16
+ this.fileExtension = options.file_extension;
17
+ this.cellType = options.cellType;
18
+ }
19
+ /**
20
+ * The foreign language.
21
+ */
22
+ readonly language: LanguageIdentifier;
23
+
24
+ /**
25
+ * Should the foreign code be appended (False) to the previously established virtual document of the same language,
26
+ * or is it standalone snippet which requires separate connection?
27
+ */
28
+ readonly standalone: boolean;
29
+
30
+ /**
31
+ * Extension of the virtual document (some servers check extensions of files), e.g. 'py' or 'R'.
32
+ */
33
+ readonly fileExtension: string;
34
+
35
+ /**
36
+ * The supported cell types.
37
+ */
38
+ readonly cellType: string[];
39
+
40
+ /**
41
+ * Test if there is any foreign code in provided code snippet.
42
+ */
43
+ hasForeignCode(code: string, cellType: string): boolean {
44
+ return this.cellType.includes(cellType);
45
+ }
46
+
47
+ /**
48
+ * Split the code into the host and foreign code (if any foreign code was detected)
49
+ */
50
+ extractForeignCode(code: string): IExtractedCode[] {
51
+ let lines = code.split('\n');
52
+
53
+ let extracts = new Array<IExtractedCode>();
54
+
55
+ let foreignCodeFragment = code;
56
+
57
+ let start = positionAtOffset(0, lines);
58
+ let end = positionAtOffset(foreignCodeFragment.length, lines);
59
+
60
+ extracts.push({
61
+ hostCode: '',
62
+ foreignCode: foreignCodeFragment,
63
+ range: { start, end },
64
+ virtualShift: null
65
+ });
66
+
67
+ return extracts;
68
+ }
69
+ }
70
+
71
+ namespace TextForeignCodeExtractor {
72
+ export interface IOptions {
73
+ /**
74
+ * The foreign language.
75
+ */
76
+ language: string;
77
+
78
+ /**
79
+ * Should the foreign code be appended (False) to the previously established virtual document of the same language,
80
+ * or is it standalone snippet which requires separate connection?
81
+ */
82
+ isStandalone: boolean;
83
+
84
+ /**
85
+ * Extension of the virtual document (some servers check extensions of files), e.g. 'py' or 'R'.
86
+ */
87
+ file_extension: string;
88
+
89
+ /**
90
+ * The supported cell types.
91
+ */
92
+ cellType: string[];
93
+ }
94
+ }
@@ -0,0 +1,78 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { CodeEditor } from '@jupyterlab/codeeditor';
5
+
6
+ import { LanguageIdentifier } from '../lsp';
7
+
8
+ export interface IExtractedCode {
9
+ /**
10
+ * Foreign code (may be empty, for example line of '%R') or null if none.
11
+ */
12
+ foreignCode: string | null;
13
+ /**
14
+ * Range of the foreign code relative to the original source.
15
+ * `null` is used internally to represent a leftover host code after extraction.
16
+ */
17
+ range: CodeEditor.IRange | null;
18
+ /**
19
+ * Shift due to any additional code inserted at the beginning of the virtual document
20
+ * (usually in order to mock the arguments passed to a magic, or to provide other context clues for the linters)
21
+ */
22
+ virtualShift: CodeEditor.IPosition | null;
23
+ /**
24
+ * Code to be retained in the virtual document of the host.
25
+ */
26
+ hostCode: string | null;
27
+ }
28
+
29
+ /**
30
+ * Foreign code extractor makes it possible to analyze code of language X embedded in code (or notebook) of language Y.
31
+ *
32
+ * The typical examples are:
33
+ * - (X=CSS< Y=HTML), or
34
+ * - (X=JavaScript, Y=HTML),
35
+ *
36
+ * while in the data analysis realm, examples include:
37
+ * - (X=R, Y=IPython),
38
+ * - (X=LATEX Y=IPython),
39
+ * - (X=SQL, Y=IPython)
40
+ *
41
+ * This extension does not aim to provide comprehensive abilities for foreign code extraction,
42
+ * but it does intend to provide stable interface for other extensions to build on it.
43
+ *
44
+ * A simple, regular expression based, configurable foreign extractor is implemented
45
+ * to provide a good reference and a good initial experience for the users.
46
+ */
47
+ export interface IForeignCodeExtractor {
48
+ /**
49
+ * The foreign language.
50
+ */
51
+ readonly language: LanguageIdentifier;
52
+
53
+ /**
54
+ * The supported cell types.
55
+ */
56
+ readonly cellType: string[];
57
+
58
+ /**
59
+ * Split the code into the host and foreign code (if any foreign code was detected)
60
+ */
61
+ extractForeignCode(code: string): IExtractedCode[];
62
+
63
+ /**
64
+ * Does the extractor produce code which should be appended to the previously established virtual document (False)
65
+ * of the same language, or does it produce standalone snippets which require separate connections (True)?
66
+ */
67
+ readonly standalone: boolean;
68
+
69
+ /**
70
+ * Test if there is any foreign code in provided code snippet.
71
+ */
72
+ hasForeignCode(code: string, cellType: string): boolean;
73
+
74
+ /**
75
+ * Extension of the virtual document (some servers check extensions of files), e.g. 'py' or 'R'.
76
+ */
77
+ readonly fileExtension: string;
78
+ }
package/src/feature.ts ADDED
@@ -0,0 +1,58 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { ISignal, Signal } from '@lumino/signaling';
5
+ import mergeWith from 'lodash.mergewith';
6
+
7
+ import { ClientCapabilities } from './lsp';
8
+ import { IFeature, ILSPFeatureManager } from './tokens';
9
+
10
+ /**
11
+ * Class to manager the registered features of the language servers.
12
+ */
13
+ export class FeatureManager implements ILSPFeatureManager {
14
+ constructor() {
15
+ this._featuresRegistered = new Signal(this);
16
+ }
17
+ /**
18
+ * List of registered features
19
+ */
20
+ readonly features: Array<IFeature> = [];
21
+
22
+ /**
23
+ * Signal emitted when a new feature is registered.
24
+ */
25
+ get featuresRegistered(): ISignal<ILSPFeatureManager, IFeature> {
26
+ return this._featuresRegistered;
27
+ }
28
+
29
+ /**
30
+ * Register a new feature, skip if it is already registered.
31
+ */
32
+ register(feature: IFeature): void {
33
+ if (this.features.some(ft => ft.id === feature.id)) {
34
+ console.warn(
35
+ `Feature with id ${feature.id} is already registered, skipping.`
36
+ );
37
+ } else {
38
+ this.features.push(feature);
39
+ this._featuresRegistered.emit(feature);
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Get the capabilities of all clients.
45
+ */
46
+ clientCapabilities(): ClientCapabilities {
47
+ let capabilities: ClientCapabilities = {};
48
+ for (const feature of this.features) {
49
+ if (!feature.capabilities) {
50
+ continue;
51
+ }
52
+ capabilities = mergeWith(capabilities, feature.capabilities);
53
+ }
54
+ return capabilities;
55
+ }
56
+
57
+ private _featuresRegistered: Signal<ILSPFeatureManager, IFeature>;
58
+ }
package/src/index.ts ADDED
@@ -0,0 +1,17 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+ /**
4
+ * @packageDocumentation
5
+ * @module lsp
6
+ */
7
+
8
+ export * from './adapters/adapter';
9
+ export * from './connection_manager';
10
+ export * from './extractors';
11
+ export * from './feature';
12
+ export * from './manager';
13
+ export * from './plugin';
14
+ export * from './positioning';
15
+ export * from './tokens';
16
+ export * from './utils';
17
+ export * from './virtual/document';
package/src/lsp.ts ADDED
@@ -0,0 +1,160 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import type * as lsp from 'vscode-languageserver-protocol';
5
+
6
+ export type ClientCapabilities = lsp.ClientCapabilities;
7
+
8
+ export enum DiagnosticSeverity {
9
+ Error = 1,
10
+ Warning = 2,
11
+ Information = 3,
12
+ Hint = 4
13
+ }
14
+
15
+ export enum DiagnosticTag {
16
+ Unnecessary = 1,
17
+ Deprecated = 2
18
+ }
19
+
20
+ export enum CompletionItemTag {
21
+ Deprecated = 1
22
+ }
23
+
24
+ export enum CompletionItemKind {
25
+ Text = 1,
26
+ Method = 2,
27
+ Function = 3,
28
+ Constructor = 4,
29
+ Field = 5,
30
+ Variable = 6,
31
+ Class = 7,
32
+ Interface = 8,
33
+ Module = 9,
34
+ Property = 10,
35
+ Unit = 11,
36
+ Value = 12,
37
+ Enum = 13,
38
+ Keyword = 14,
39
+ Snippet = 15,
40
+ Color = 16,
41
+ File = 17,
42
+ Reference = 18,
43
+ Folder = 19,
44
+ EnumMember = 20,
45
+ Constant = 21,
46
+ Struct = 22,
47
+ Event = 23,
48
+ Operator = 24,
49
+ TypeParameter = 25
50
+ }
51
+
52
+ export enum DocumentHighlightKind {
53
+ Text = 1,
54
+ Read = 2,
55
+ Write = 3
56
+ }
57
+
58
+ export enum CompletionTriggerKind {
59
+ Invoked = 1,
60
+ TriggerCharacter = 2,
61
+ TriggerForIncompleteCompletions = 3
62
+ }
63
+
64
+ export enum AdditionalCompletionTriggerKinds {
65
+ AutoInvoked = 9999
66
+ }
67
+
68
+ export type ExtendedCompletionTriggerKind =
69
+ | CompletionTriggerKind
70
+ | AdditionalCompletionTriggerKinds;
71
+
72
+ export type CompletionItemKindStrings = keyof typeof CompletionItemKind;
73
+
74
+ /**
75
+ * The language identifier for LSP, with the preferred identifier as defined in the documentation
76
+ * see the table in https://microsoft.github.io/language-server-protocol/specification#textDocumentItem
77
+ */
78
+ export enum Languages {
79
+ 'abap' = 'ABAP',
80
+ 'bat' = 'Windows Bat',
81
+ 'bibtex' = 'BibTeX',
82
+ 'clojure' = 'Clojure',
83
+ 'coffeescript' = 'Coffeescript',
84
+ 'c' = 'C',
85
+ 'cpp' = 'C++',
86
+ 'csharp' = 'C#',
87
+ 'css' = 'CSS',
88
+ 'diff' = 'Diff',
89
+ 'dart' = 'Dart',
90
+ 'dockerfile' = 'Dockerfile',
91
+ 'elixir' = 'Elixir',
92
+ 'erlang' = 'Erlang',
93
+ 'fsharp' = 'F#',
94
+ 'git-commit' = 'Git (commit)',
95
+ 'git-rebase' = 'Git (rebase)',
96
+ 'go' = 'Go',
97
+ 'groovy' = 'Groovy',
98
+ 'handlebars' = 'Handlebars',
99
+ 'html' = 'HTML',
100
+ 'ini' = 'Ini',
101
+ 'java' = 'Java',
102
+ 'javascript' = 'JavaScript',
103
+ 'javascriptreact' = 'JavaScript React',
104
+ 'json' = 'JSON',
105
+ 'latex' = 'LaTeX',
106
+ 'less' = 'Less',
107
+ 'lua' = 'Lua',
108
+ 'makefile' = 'Makefile',
109
+ 'markdown' = 'Markdown',
110
+ 'objective-c' = 'Objective-C',
111
+ 'objective-cpp' = 'Objective-C++',
112
+ 'perl' = 'Perl',
113
+ 'perl6' = 'Perl 6',
114
+ 'php' = 'PHP',
115
+ 'powershell' = 'Powershell',
116
+ 'jade' = 'Pug',
117
+ 'python' = 'Python',
118
+ 'r' = 'R',
119
+ 'razor' = 'Razor (cshtml)',
120
+ 'ruby' = 'Ruby',
121
+ 'rust' = 'Rust',
122
+ 'scss' = 'SCSS (syntax using curly brackets)',
123
+ 'sass' = 'SCSS (indented syntax)',
124
+ 'scala' = 'Scala',
125
+ 'shaderlab' = 'ShaderLab',
126
+ 'shellscript' = 'Shell Script (Bash)',
127
+ 'sql' = 'SQL',
128
+ 'swift' = 'Swift',
129
+ 'typescript' = 'TypeScript',
130
+ 'typescriptreact' = 'TypeScript React',
131
+ 'tex' = 'TeX',
132
+ 'vb' = 'Visual Basic',
133
+ 'xml' = 'XML',
134
+ 'xsl' = 'XSL',
135
+ 'yaml' = 'YAML'
136
+ }
137
+
138
+ export type RecommendedLanguageIdentifier = keyof typeof Languages;
139
+
140
+ /**
141
+ * Language identifier for the LSP server, allowing any string but preferring
142
+ * the identifiers as recommended by the LSP documentation.
143
+ */
144
+ export type LanguageIdentifier = RecommendedLanguageIdentifier | string;
145
+
146
+ /**
147
+ * Type represents a location inside a resource, such as a line
148
+ * inside a text file.
149
+ */
150
+ export type AnyLocation =
151
+ | lsp.Location
152
+ | lsp.Location[]
153
+ | lsp.LocationLink[]
154
+ | undefined
155
+ | null;
156
+
157
+ /**
158
+ * Type represents the completion result.
159
+ */
160
+ export type AnyCompletion = lsp.CompletionList | lsp.CompletionItem[];