Skip to content

Commit 8c380f6

Browse files
committed
Document preferred language ids and register() methods
1 parent a852e87 commit 8c380f6

File tree

7 files changed

+100
-7
lines changed

7 files changed

+100
-7
lines changed

packages/jupyterlab-lsp/src/adapters/adapter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ILSPExtension } from '../index';
1717
import { IFeatureEditorIntegration, IFeature } from '../feature';
1818
import { EditorAdapter } from '../editor_integration/editor_adapter';
1919
import IEditor = CodeEditor.IEditor;
20+
import { LanguageIdentifier } from '../lsp';
2021

2122
export class StatusMessage {
2223
/**
@@ -195,8 +196,9 @@ export abstract class WidgetAdapter<T extends IDocumentWidget> {
195196
return this.widget.id;
196197
}
197198

198-
get language(): string {
199-
// the values should follow https://microsoft.github.io/language-server-protocol/specification guidelines
199+
get language(): LanguageIdentifier {
200+
// the values should follow https://microsoft.github.io/language-server-protocol/specification guidelines,
201+
// see the table in https://microsoft.github.io/language-server-protocol/specification#textDocumentItem
200202
if (mime_type_language_map.hasOwnProperty(this.mime_type)) {
201203
return mime_type_language_map[this.mime_type] as string;
202204
} else {

packages/jupyterlab-lsp/src/extractors/manager.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export class CodeExtractorsManager implements ILSPCodeExtractorsManager {
99
this.registry = {};
1010
}
1111

12+
/**
13+
* Register an extractor to extract foreign code from host documents of specified language.
14+
*/
1215
register(extractor: IForeignCodeExtractor, host_language: string): void {
1316
if (!this.registry.hasOwnProperty(host_language)) {
1417
this.registry[host_language] = [];

packages/jupyterlab-lsp/src/extractors/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CodeEditor } from '@jupyterlab/codeeditor';
2+
import { LanguageIdentifier } from '../lsp';
23

34
export interface IExtractedCode {
45
/**
@@ -40,9 +41,9 @@ export interface IExtractedCode {
4041
*/
4142
export interface IForeignCodeExtractor {
4243
/**
43-
* The foreign language. TODO what is language, what is host language, ILanguageInfo
44+
* The foreign language.
4445
*/
45-
language: string;
46+
language: LanguageIdentifier;
4647

4748
/**
4849
* Split the code into the host and foreign code (if any foreign code was detected)

packages/jupyterlab-lsp/src/lsp.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,75 @@ export enum CompletionTriggerKind {
4646
}
4747

4848
export type CompletionItemKindStrings = keyof typeof CompletionItemKind;
49+
50+
/**
51+
* The language identifier for LSP, with the preferred identifier as defined in the documentation
52+
* see the table in https://microsoft.github.io/language-server-protocol/specification#textDocumentItem
53+
*/
54+
export enum Languages {
55+
'abap' = 'ABAP',
56+
'bat' = 'Windows Bat',
57+
'bibtex' = 'BibTeX',
58+
'clojure' = 'Clojure',
59+
'coffeescript' = 'Coffeescript',
60+
'c' = 'C',
61+
'cpp' = 'C++',
62+
'csharp' = 'C#',
63+
'css' = 'CSS',
64+
'diff' = 'Diff',
65+
'dart' = 'Dart',
66+
'dockerfile' = 'Dockerfile',
67+
'elixir' = 'Elixir',
68+
'erlang' = 'Erlang',
69+
'fsharp' = 'F#',
70+
'git-commit' = 'Git (commit)',
71+
'git-rebase' = 'Git (rebase)',
72+
'go' = 'Go',
73+
'groovy' = 'Groovy',
74+
'handlebars' = 'Handlebars',
75+
'html' = 'HTML',
76+
'ini' = 'Ini',
77+
'java' = 'Java',
78+
'javascript' = 'JavaScript',
79+
'javascriptreact' = 'JavaScript React',
80+
'json' = 'JSON',
81+
'latex' = 'LaTeX',
82+
'less' = 'Less',
83+
'lua' = 'Lua',
84+
'makefile' = 'Makefile',
85+
'markdown' = 'Markdown',
86+
'objective-c' = 'Objective-C',
87+
'objective-cpp' = 'Objective-C++',
88+
'perl' = 'Perl',
89+
'perl6' = 'Perl 6',
90+
'php' = 'PHP',
91+
'powershell' = 'Powershell',
92+
'jade' = 'Pug',
93+
'python' = 'Python',
94+
'r' = 'R',
95+
'razor' = 'Razor (cshtml)',
96+
'ruby' = 'Ruby',
97+
'rust' = 'Rust',
98+
'scss' = 'SCSS (syntax using curly brackets)',
99+
'sass' = 'SCSS (indented syntax)',
100+
'scala' = 'Scala',
101+
'shaderlab' = 'ShaderLab',
102+
'shellscript' = 'Shell Script (Bash)',
103+
'sql' = 'SQL',
104+
'swift' = 'Swift',
105+
'typescript' = 'TypeScript',
106+
'typescriptreact' = 'TypeScript React',
107+
'tex' = 'TeX',
108+
'vb' = 'Visual Basic',
109+
'xml' = 'XML',
110+
'xsl' = 'XSL',
111+
'yaml' = 'YAML'
112+
}
113+
114+
export type RecommendedLanguageIdentifier = keyof typeof Languages;
115+
116+
/**
117+
* Language identifier for the LSP server, allowing any string but preferring
118+
* the identifiers as recommended by the LSP documentation.
119+
*/
120+
export type LanguageIdentifier = RecommendedLanguageIdentifier | string;

packages/jupyterlab-lsp/src/overrides/tokens.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PLUGIN_ID } from '../tokens';
22
import { Token } from '@lumino/coreutils';
3+
import { LanguageIdentifier } from '../lsp';
34

45
export type replacer = (...args: string[]) => string;
56

@@ -44,7 +45,10 @@ export interface IScopedCodeOverride extends ICodeOverride {
4445
*/
4546
export interface ILSPCodeOverridesManager {
4647
readonly registry: ICodeOverridesRegistry;
47-
register(override: IScopedCodeOverride, language: string): void;
48+
/**
49+
* Register a code override to replace code fragments in documents of specified language.
50+
*/
51+
register(override: IScopedCodeOverride, language: LanguageIdentifier): void;
4852
}
4953

5054
export const ILSPCodeOverridesManager = new Token<ILSPCodeOverridesManager>(

packages/jupyterlab-lsp/src/tokens.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
IForeignCodeExtractor,
2222
IForeignCodeExtractorsRegistry
2323
} from './extractors/types';
24+
import { LanguageIdentifier } from './lsp';
2425

2526
export type TLanguageServerId = string;
2627
export type TLanguageId = string;
@@ -173,7 +174,10 @@ export interface ILSPCodeExtractorsManager {
173174
/**
174175
* Register the extraction rules to be applied in documents with language `host_language`.
175176
*/
176-
register(extractor: IForeignCodeExtractor, host_language: string): void;
177+
register(
178+
extractor: IForeignCodeExtractor,
179+
host_language: LanguageIdentifier
180+
): void;
177181
}
178182

179183
export const PLUGIN_ID = '@krassowski/jupyterlab-lsp';

packages/jupyterlab-lsp/src/virtual/document.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { DocumentConnectionManager } from '../connection_manager';
1717
import { create_console, EditorLogConsole } from './console';
1818
import IRange = CodeEditor.IRange;
1919
import { ReversibleOverridesMap } from '../overrides/maps';
20+
import { LanguageIdentifier } from '../lsp';
2021

2122
type language = string;
2223

@@ -124,11 +125,17 @@ export class VirtualDocumentInfo implements IDocumentInfo {
124125

125126
export namespace VirtualDocument {
126127
export interface IOptions {
127-
language: string;
128+
language: LanguageIdentifier;
128129
foreign_code_extractors: IForeignCodeExtractorsRegistry;
129130
overrides_registry: ICodeOverridesRegistry;
130131
path: string;
131132
file_extension: string;
133+
/**
134+
* Notebooks or any other aggregates of documents are not supported
135+
* by the LSP specification, and we need to make appropriate
136+
* adjustments for them, pretending they are simple files
137+
* so that the LSP servers do not refuse to cooperate.
138+
*/
132139
has_lsp_supported_file: boolean;
133140
/**
134141
* Being standalone is relevant to foreign documents

0 commit comments

Comments
 (0)