Skip to content

Commit c6c9c8f

Browse files
authored
Merge pull request #179 from ayame113/configurable-language-id
2 parents db6c96b + e1bd570 commit c6c9c8f

File tree

4 files changed

+72
-12
lines changed

4 files changed

+72
-12
lines changed

lib/adapters/document-sync-adapter.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,19 @@ export default class DocumentSyncAdapter {
5555
/**
5656
* Public: Create a new {DocumentSyncAdapter} for the given language server.
5757
*
58-
* @param connection A {LanguageClientConnection} to the language server to be kept in sync.
58+
* @param _connection A {LanguageClientConnection} to the language server to be kept in sync.
5959
* @param documentSync The document syncing options.
60-
* @param editorSelector A predicate function that takes a {TextEditor} and returns a {boolean} indicating whether
60+
* @param _editorSelector A predicate function that takes a {TextEditor} and returns a {boolean} indicating whether
6161
* this adapter should care about the contents of the editor.
62+
* @param _getLanguageIdFromEditor A function that returns a {string} of `languageId` used for `textDocument/didOpen`
63+
* notification.
6264
*/
6365
constructor(
6466
private _connection: LanguageClientConnection,
6567
private _editorSelector: (editor: TextEditor) => boolean,
6668
documentSync: TextDocumentSyncOptions | TextDocumentSyncKind | undefined,
67-
private _reportBusyWhile: Utils.ReportBusyWhile
69+
private _reportBusyWhile: Utils.ReportBusyWhile,
70+
private _getLanguageIdFromEditor: (editor: TextEditor) => string
6871
) {
6972
if (typeof documentSync === "object") {
7073
this._documentSync = documentSync
@@ -119,7 +122,8 @@ export default class DocumentSyncAdapter {
119122
this._connection,
120123
this._documentSync,
121124
this._versions,
122-
this._reportBusyWhile
125+
this._reportBusyWhile,
126+
this._getLanguageIdFromEditor
123127
)
124128
this._editors.set(editor, sync)
125129
this._disposable.add(sync)
@@ -149,16 +153,17 @@ export class TextEditorSyncAdapter {
149153
/**
150154
* Public: Create a {TextEditorSyncAdapter} in sync with a given language server.
151155
*
152-
* @param editor A {TextEditor} to keep in sync.
153-
* @param connection A {LanguageClientConnection} to a language server to keep in sync.
154-
* @param documentSync The document syncing options.
156+
* @param _editor A {TextEditor} to keep in sync.
157+
* @param _connection A {LanguageClientConnection} to a language server to keep in sync.
158+
* @param _documentSync The document syncing options.
155159
*/
156160
constructor(
157161
private _editor: TextEditor,
158162
private _connection: LanguageClientConnection,
159163
private _documentSync: TextDocumentSyncOptions,
160164
private _versions: Map<string, number>,
161-
private _reportBusyWhile: Utils.ReportBusyWhile
165+
private _reportBusyWhile: Utils.ReportBusyWhile,
166+
private _getLanguageIdFromEditor: (editor: TextEditor) => string
162167
) {
163168
this._fakeDidChangeWatchedFiles = atom.project.onDidChangeFiles == null
164169

@@ -203,9 +208,9 @@ export class TextEditorSyncAdapter {
203208
this._disposable.dispose()
204209
}
205210

206-
/** Get the languageId field that will be sent to the language server by simply using the grammar name. */
211+
/** Get the languageId field that will be sent to the language server by simply using the `_getLanguageIdFromEditor`. */
207212
public getLanguageId(): string {
208-
return this._editor.getGrammar().name
213+
return this._getLanguageIdFromEditor(this._editor)
209214
}
210215

211216
/**

lib/auto-languageclient.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,28 @@ export default class AutoLanguageClient {
295295
return configuration
296296
}
297297

298+
/**
299+
* (Optional) Determines the `languageId` string used for `textDocument/didOpen` notification. The default is to use
300+
* the grammar name.
301+
*
302+
* You can override this like this:
303+
*
304+
* class MyLanguageClient extends AutoLanguageClient {
305+
* getLanguageIdFromEditor(editor: TextEditor) {
306+
* if (editor.getGrammar().scopeName === "source.myLanguage") {
307+
* return "myCustumLanguageId"
308+
* }
309+
* return super.getLanguageIdFromEditor(editor)
310+
* }
311+
* }
312+
*
313+
* @param editor A {TextEditor} which is opened.
314+
* @returns A {string} of `languageId` used for `textDocument/didOpen` notification.
315+
*/
316+
protected getLanguageIdFromEditor(editor: TextEditor): string {
317+
return editor.getGrammar().name
318+
}
319+
298320
// Helper methods that are useful for implementors
299321
// ---------------------------------------------------------------------------
300322

@@ -576,7 +598,8 @@ export default class AutoLanguageClient {
576598
server.connection,
577599
(editor) => this.shouldSyncForEditor(editor, server.projectPath),
578600
server.capabilities.textDocumentSync,
579-
this.reportBusyWhile
601+
this.reportBusyWhile,
602+
(editor) => this.getLanguageIdFromEditor(editor)
580603
)
581604
server.disposable.add(docSyncAdapter)
582605
}

test/adapters/document-sync-adapter.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TextDocumentSyncKind, TextDocumentSyncOptions } from "../../lib/languageclient"
22
import DocumentSyncAdapter from "../../lib/adapters/document-sync-adapter"
3+
import { createFakeEditor } from "../helpers"
34

45
describe("DocumentSyncAdapter", () => {
56
describe("canAdapt", () => {
@@ -52,7 +53,8 @@ describe("DocumentSyncAdapter", () => {
5253
null as any,
5354
() => false,
5455
textDocumentSync,
55-
(_t, f) => f()
56+
(_t, f) => f(),
57+
() => ""
5658
)
5759
}
5860

@@ -81,4 +83,24 @@ describe("DocumentSyncAdapter", () => {
8183
expect(result).toBe(TextDocumentSyncKind.Full)
8284
})
8385
})
86+
87+
describe("getLanguageId", () => {
88+
function create(getLanguageIdFromEditor: () => string) {
89+
return new DocumentSyncAdapter(
90+
null as any,
91+
() => false,
92+
TextDocumentSyncKind.Incremental,
93+
(_t, f) => f(),
94+
getLanguageIdFromEditor
95+
)
96+
}
97+
98+
it("use the return value of `_getLanguageIdFromEditor` as the languageId", () => {
99+
const editor = createFakeEditor()
100+
const adapter = create(() => "someLanguageId") as any
101+
adapter._handleNewEditor(editor)
102+
const result = adapter.getEditorSyncAdapter(editor).getLanguageId()
103+
expect(result).toBe("someLanguageId")
104+
})
105+
})
84106
})

test/auto-languageclient.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,14 @@ describe("AutoLanguageClient", () => {
209209
expect(client.shouldSyncForEditor(editor, "/path/to/somewhere")).toBe(false)
210210
})
211211
})
212+
213+
describe("getLanguageIdFromEditor", () => {
214+
const client = setupClient()
215+
216+
it("returns the editor's grammar name", () => {
217+
const editor = new TextEditor()
218+
spyOn(editor, "getGrammar").and.returnValue({ name: "testGrammarName" } as any)
219+
expect((client as any).getLanguageIdFromEditor(editor)).toBe("testGrammarName")
220+
})
221+
})
212222
})

0 commit comments

Comments
 (0)