Skip to content

Commit ff1aa7a

Browse files
aminyaillright
andcommitted
feat: implement window/showDocument
Co-Authored-By: Lev Chelyadinov <[email protected]>
1 parent 5f462f1 commit ff1aa7a

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

lib/adapters/show-document-adapter.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { shell } from "electron"
2+
import { LanguageClientConnection, ShowDocumentParams, ShowDocumentResult } from "../languageclient"
3+
import { TextEditor } from "atom"
4+
import Convert from "../convert"
5+
6+
/** Public: Adapts the window/showDocument command to Atom's text editors or external programs. */
7+
export default class ShowDocumentAdapter {
8+
/**
9+
* Public: Attach to a {LanguageClientConnection} to recieve requests to show documents.
10+
*/
11+
public static attach(connection: LanguageClientConnection): void {
12+
connection.onShowDocument(ShowDocumentAdapter.onShowDocument)
13+
}
14+
15+
/**
16+
* Public: Show a notification message with buttons using the Atom notifications API.
17+
*
18+
* @param params The {ShowDocumentParams} received from the language server
19+
* indicating the document to be displayed as well as other metadata.
20+
*/
21+
public static async onShowDocument(params: ShowDocumentParams): Promise<ShowDocumentResult> {
22+
if (!params.external) {
23+
// open using atom.workspace
24+
const view = await atom.workspace.open(params.uri, {
25+
activateItem: params.takeFocus,
26+
activatePane: params.takeFocus,
27+
pending: true,
28+
initialLine: params.selection?.start.line ?? 0,
29+
initialColumn: params.selection?.start.character ?? 0,
30+
})
31+
if (view instanceof TextEditor && params.selection != null) {
32+
view.selectToBufferPosition(Convert.positionToPoint(params.selection.end))
33+
}
34+
return { success: true }
35+
} else {
36+
// open using Electron
37+
shell.openExternal(params.uri, { activate: params.takeFocus })
38+
return { success: false }
39+
}
40+
}
41+
}

lib/auto-languageclient.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import NotificationsAdapter from "./adapters/notifications-adapter"
2121
import OutlineViewAdapter from "./adapters/outline-view-adapter"
2222
import RenameAdapter from "./adapters/rename-adapter"
2323
import SignatureHelpAdapter from "./adapters/signature-help-adapter"
24+
import ShowDocumentAdapter from "./adapters/show-document-adapter"
2425
import * as Utils from "./utils"
2526
import { Socket } from "net"
2627
import { LanguageClientConnection } from "./languageclient"
@@ -197,6 +198,11 @@ export default class AutoLanguageClient {
197198
colorProvider: undefined,
198199
foldingRange: undefined,
199200
},
201+
window: {
202+
workDoneProgress: false, // TODO: support
203+
showMessage: undefined,
204+
showDocument: { support: true },
205+
},
200206
experimental: {},
201207
},
202208
}
@@ -506,6 +512,8 @@ export default class AutoLanguageClient {
506512
loggingConsole,
507513
signatureHelpAdapter,
508514
})
515+
516+
ShowDocumentAdapter.attach(server.connection)
509517
}
510518

511519
public shouldSyncForEditor(editor: TextEditor, projectPath: string): boolean {

lib/languageclient.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface KnownNotifications {
1515
}
1616

1717
export interface KnownRequests {
18+
"window/showDocument": [lsp.ShowDocumentParams, lsp.ShowDocumentResult]
1819
"window/showMessageRequest": [lsp.ShowMessageRequestParams, lsp.MessageActionItem | null]
1920
"workspace/applyEdit": [lsp.ApplyWorkspaceEditParams, lsp.ApplyWorkspaceEditResponse]
2021
[custom: string]: [Record<string, any>, Record<string, any> | null]
@@ -166,6 +167,16 @@ export class LanguageClientConnection extends EventEmitter {
166167
this._onRequest({ method: "window/showMessageRequest" }, callback)
167168
}
168169

170+
/**
171+
* Public: Register a callback for the `window/showDocument` message.
172+
*
173+
* @param callback The function to be called when the `window/showDocument` message is
174+
* received with {ShowDocumentParams} being passed.
175+
*/
176+
public onShowDocument(callback: (params: lsp.ShowDocumentParams) => Promise<lsp.ShowDocumentResult>): void {
177+
this._onRequest({ method: "window/showDocument" }, callback)
178+
}
179+
169180
/**
170181
* Public: Register a callback for the `window/logMessage` message.
171182
*

typings/electron/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
interface OpenExternalOptions {
2+
activate?: boolean
3+
workingDirectory?: string
4+
}
5+
6+
declare module "electron" {
7+
export const shell: {
8+
openExternal(url: string, options?: OpenExternalOptions): void
9+
}
10+
}

0 commit comments

Comments
 (0)