Skip to content

Commit 68b1d5a

Browse files
authored
Merge pull request #359 from TypeFox/fix-code-converters
Add some typing on the code/proto converters
2 parents 315e3f9 + 34055e8 commit 68b1d5a

2 files changed

Lines changed: 158 additions & 37 deletions

File tree

packages/client/src/converters.ts

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import * as p2c from 'vscode-languageclient/lib/common/protocolConverter';
2+
import * as c2p from 'vscode-languageclient/lib/common/codeConverter';
3+
import { WillSaveTextDocumentParams } from 'vscode-languageclient';
4+
import * as code from 'vscode';
5+
import * as proto from 'vscode-languageserver-protocol';
6+
7+
type NotAPromise<T> = T extends object & { then(onfulfilled: infer F): any } ? F extends ((value: infer V, ...args: any) => any) ? NotAPromise<V> : never : T;
8+
9+
function bypassConversion<R>(param: any): NotAPromise<R> {
10+
return param
11+
}
12+
13+
function toPromise<R>(param: any): Promise<R> {
14+
return Promise.resolve(param)
15+
}
16+
17+
export class MonacoP2CConverter implements p2c.Converter {
18+
constructor(private delegate: p2c.Converter) { }
19+
20+
asUri = this.delegate.asUri.bind(this.delegate)
21+
asDocumentSelector = bypassConversion
22+
asPosition = bypassConversion
23+
asRange = bypassConversion
24+
asRanges = toPromise
25+
asDiagnostic = bypassConversion
26+
asDiagnostics = toPromise
27+
asDiagnosticSeverity = bypassConversion
28+
asDiagnosticTag = bypassConversion
29+
asHover = bypassConversion
30+
asCompletionResult = bypassConversion
31+
asCompletionItem = bypassConversion
32+
asTextEdit = bypassConversion
33+
asTextEdits = bypassConversion
34+
asSignatureHelp = bypassConversion
35+
asSignatureInformation = toPromise
36+
asSignatureInformations = toPromise
37+
asParameterInformation = bypassConversion
38+
asParameterInformations = toPromise
39+
asLocation = bypassConversion
40+
asDeclarationResult = bypassConversion
41+
asDefinitionResult = bypassConversion
42+
asReferences = bypassConversion
43+
asDocumentHighlightKind = bypassConversion
44+
asDocumentHighlight = bypassConversion
45+
asDocumentHighlights = bypassConversion
46+
asSymbolKind = bypassConversion
47+
asSymbolTag = bypassConversion
48+
asSymbolTags = bypassConversion
49+
asSymbolInformation = bypassConversion
50+
asSymbolInformations = bypassConversion
51+
asDocumentSymbol = bypassConversion
52+
asDocumentSymbols = bypassConversion
53+
asCommand = bypassConversion
54+
asCommands = bypassConversion
55+
asCodeAction = bypassConversion
56+
asCodeActionKind = bypassConversion
57+
asCodeActionKinds = bypassConversion
58+
asCodeActionResult = toPromise
59+
asCodeLens = bypassConversion
60+
asCodeLenses = bypassConversion
61+
asWorkspaceEdit = bypassConversion
62+
asDocumentLink = bypassConversion
63+
asDocumentLinks = bypassConversion
64+
asColor = bypassConversion
65+
asColorInformation = bypassConversion
66+
asColorInformations = bypassConversion
67+
asColorPresentation = bypassConversion
68+
asColorPresentations = bypassConversion
69+
asFoldingRangeKind = bypassConversion
70+
asFoldingRange = bypassConversion
71+
asFoldingRanges = bypassConversion
72+
asSelectionRange = bypassConversion
73+
asSelectionRanges = bypassConversion
74+
asInlineValue = bypassConversion
75+
asInlineValues = bypassConversion
76+
asInlayHint = toPromise
77+
asInlayHints = bypassConversion
78+
asSemanticTokensLegend = bypassConversion
79+
asSemanticTokens = bypassConversion
80+
asSemanticTokensEdit = bypassConversion
81+
asSemanticTokensEdits = bypassConversion
82+
asCallHierarchyItem = bypassConversion
83+
asCallHierarchyItems = bypassConversion
84+
asCallHierarchyIncomingCall = toPromise
85+
asCallHierarchyIncomingCalls = bypassConversion
86+
asCallHierarchyOutgoingCall = toPromise
87+
asCallHierarchyOutgoingCalls = bypassConversion
88+
asLinkedEditingRanges = bypassConversion
89+
asTypeHierarchyItem = bypassConversion
90+
asTypeHierarchyItems = bypassConversion
91+
asGlobPattern = bypassConversion
92+
}
93+
94+
export class MonacoC2PConverter implements c2p.Converter {
95+
constructor(private delegate: c2p.Converter) { }
96+
97+
asUri = this.delegate.asUri.bind(this.delegate)
98+
asTextDocumentItem = bypassConversion
99+
asTextDocumentIdentifier = bypassConversion
100+
asVersionedTextDocumentIdentifier = bypassConversion
101+
asOpenTextDocumentParams = this.delegate.asOpenTextDocumentParams.bind(this.delegate)
102+
asChangeTextDocumentParams = this.delegate.asChangeTextDocumentParams.bind(this.delegate)
103+
asCloseTextDocumentParams = this.delegate.asCloseTextDocumentParams.bind(this.delegate)
104+
asSaveTextDocumentParams = this.delegate.asSaveTextDocumentParams.bind(this.delegate)
105+
asWillSaveTextDocumentParams = (event: code.TextDocumentWillSaveEvent): WillSaveTextDocumentParams => {
106+
return {
107+
textDocument: this.delegate.asTextDocumentIdentifier(event.document),
108+
reason: event.reason
109+
}
110+
}
111+
asDidCreateFilesParams = this.delegate.asDidCreateFilesParams.bind(this.delegate)
112+
asDidRenameFilesParams = this.delegate.asDidRenameFilesParams.bind(this.delegate)
113+
asDidDeleteFilesParams = this.delegate.asDidDeleteFilesParams.bind(this.delegate)
114+
asWillCreateFilesParams = this.delegate.asWillCreateFilesParams.bind(this.delegate)
115+
asWillRenameFilesParams = this.delegate.asWillRenameFilesParams.bind(this.delegate)
116+
asWillDeleteFilesParams = this.delegate.asWillDeleteFilesParams.bind(this.delegate)
117+
asTextDocumentPositionParams = this.delegate.asTextDocumentPositionParams.bind(this.delegate)
118+
asCompletionParams = (textDocument: code.TextDocument, position: code.Position, context: code.CompletionContext): proto.CompletionParams => {
119+
return {
120+
textDocument: this.delegate.asTextDocumentIdentifier(textDocument),
121+
position,
122+
context
123+
} as proto.CompletionParams
124+
}
125+
asSignatureHelpParams = this.delegate.asSignatureHelpParams.bind(this.delegate)
126+
asWorkerPosition = bypassConversion
127+
asPosition = bypassConversion
128+
asPositions = toPromise
129+
asRange = bypassConversion
130+
asLocation = bypassConversion
131+
asDiagnosticSeverity = bypassConversion
132+
asDiagnosticTag = bypassConversion
133+
asDiagnostic = bypassConversion
134+
asDiagnostics = toPromise
135+
asCompletionItem = bypassConversion
136+
asSymbolKind = bypassConversion
137+
asSymbolTag = bypassConversion
138+
asSymbolTags = bypassConversion
139+
asTextEdit = bypassConversion
140+
asReferenceParams = this.delegate.asReferenceParams.bind(this.delegate)
141+
asCodeAction = toPromise
142+
asCodeActionContext = toPromise
143+
asInlineValueContext = bypassConversion
144+
asCommand = bypassConversion
145+
asCodeLens = bypassConversion
146+
asFormattingOptions = bypassConversion
147+
asDocumentSymbolParams = this.delegate.asDocumentSymbolParams.bind(this.delegate)
148+
asCodeLensParams = this.delegate.asCodeLensParams.bind(this.delegate)
149+
asDocumentLink = bypassConversion
150+
asDocumentLinkParams = this.delegate.asDocumentLinkParams.bind(this.delegate)
151+
asCallHierarchyItem = bypassConversion
152+
asTypeHierarchyItem = bypassConversion
153+
asWorkspaceSymbol = bypassConversion
154+
asInlayHint = bypassConversion
155+
}

packages/client/src/monaco-language-client.ts

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
import * as p2c from 'vscode-languageclient/lib/common/protocolConverter';
99
import * as c2p from 'vscode-languageclient/lib/common/codeConverter';
1010
import { IConnectionProvider } from './connection';
11-
import { CompletionParams, WillSaveTextDocumentParams } from './services'
1211

1312
export * from 'vscode-languageclient/lib/common/client';
1413
import type * as vscode from 'vscode'
14+
import { MonacoC2PConverter, MonacoP2CConverter } from "./converters";
1515

1616
export class MonacoLanguageClient extends BaseLanguageClient {
1717

@@ -28,42 +28,8 @@ export class MonacoLanguageClient extends BaseLanguageClient {
2828
_p2c: p2c.Converter,
2929
_c2p: c2p.Converter
3030
} = this as any;
31-
self._p2c = new Proxy(self._p2c, {
32-
get: (target: any, prop: string) => {
33-
if (prop === 'asUri') {
34-
return target[prop];
35-
}
36-
return MonacoLanguageClient.bypassConversion;
37-
}
38-
});
39-
self._c2p = new Proxy(self._c2p, {
40-
get: (target: c2p.Converter, prop: string) => {
41-
if (prop === 'asUri') {
42-
return target[prop];
43-
}
44-
if (prop === 'asCompletionParams') {
45-
return (textDocument: any, position: any, context: any): CompletionParams => {
46-
return {
47-
textDocument: target.asTextDocumentIdentifier(textDocument),
48-
position,
49-
context
50-
}
51-
}
52-
}
53-
if (prop === 'asWillSaveTextDocumentParams') {
54-
return (event: any): WillSaveTextDocumentParams => {
55-
return {
56-
textDocument: target.asTextDocumentIdentifier(event.document),
57-
reason: event.reason
58-
}
59-
}
60-
}
61-
if (prop.endsWith('Params')) {
62-
return (target as any)[prop];
63-
}
64-
return MonacoLanguageClient.bypassConversion;
65-
}
66-
});
31+
self._p2c = new MonacoP2CConverter(self._p2c);
32+
self._c2p = new MonacoC2PConverter(self._c2p);
6733
}
6834

6935
protected createMessageTransports(encoding: string): Promise<MessageTransports> {

0 commit comments

Comments
 (0)