-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdatatip-adapter.ts
More file actions
90 lines (79 loc) · 3.45 KB
/
datatip-adapter.ts
File metadata and controls
90 lines (79 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import type * as atomIde from "atom-ide-base"
import Convert from "../convert"
import * as Utils from "../utils"
import { Hover, LanguageClientConnection, MarkupContent, MarkedString, ServerCapabilities } from "../languageclient"
import { Point, TextEditor } from "atom"
/** Public: Adapts the language server protocol "textDocument/hover" to the Atom IDE UI Datatip package. */
export default class DatatipAdapter {
/**
* Public: Determine whether this adapter can be used to adapt a language server based on the serverCapabilities
* matrix containing a hoverProvider.
*
* @param serverCapabilities The {ServerCapabilities} of the language server to consider.
* @returns A {Boolean} indicating adapter can adapt the server based on the given serverCapabilities.
*/
public static canAdapt(serverCapabilities: ServerCapabilities): boolean {
return serverCapabilities.hoverProvider === true
}
/**
* Public: Get the Datatip for this {Point} in a {TextEditor} by querying the language server.
*
* @param connection A {LanguageClientConnection} to the language server that will be queried for the hover text/datatip.
* @param editor The Atom {TextEditor} containing the text the Datatip should relate to.
* @param point The Atom {Point} containing the point within the text the Datatip should relate to.
* @returns A {Promise} containing the {Datatip} to display or {null} if no Datatip is available.
*/
public async getDatatip(
connection: LanguageClientConnection,
editor: TextEditor,
point: Point
): Promise<atomIde.Datatip | null> {
const documentPositionParams = Convert.editorToTextDocumentPositionParams(editor, point)
const hover = await connection.hover(documentPositionParams)
if (hover === null || DatatipAdapter.isEmptyHover(hover)) {
return null
}
const range = hover.range === undefined ? Utils.getWordAtPosition(editor, point) : Convert.lsRangeToAtomRange(hover.range)
const markedStrings = (Array.isArray(hover.contents) ? hover.contents : [hover.contents]).map((str) =>
DatatipAdapter.convertMarkedString(editor, str)
)
return { range, markedStrings }
}
private static isEmptyHover(hover: Hover): boolean {
// TODO hover.contents is never null!
return (
hover.contents == null ||
(typeof hover.contents === "string" && hover.contents.length === 0) ||
(Array.isArray(hover.contents) && (hover.contents.length === 0 || hover.contents[0] === ""))
)
}
private static convertMarkedString(
editor: TextEditor,
markedString: MarkedString | MarkupContent
): atomIde.MarkedString {
if (typeof markedString === "string") {
return { type: "markdown", value: markedString }
}
if ((markedString as MarkupContent).kind) {
return {
type: "markdown",
value: markedString.value,
}
}
// Must check as <{language: string}> to disambiguate between
// string and the more explicit object type because MarkedString
// is a union of the two types
if ((markedString as { language: string }).language) {
return {
type: "snippet",
// TODO: find a better mapping from language -> grammar
grammar:
atom.grammars.grammarForScopeName(`source.${(markedString as { language: string }).language}`) ||
editor.getGrammar(),
value: markedString.value,
}
}
// Catch-all case
return { type: "markdown", value: markedString.toString() }
}
}