7
7
/// <reference path="../typings/promise.d.ts" />
8
8
/// <reference path="../node_modules/@types/node/index.d.ts" />
9
9
10
+ import * as ng from '@angular/language-service' ;
11
+
10
12
import 'reflect-metadata' ;
11
13
import {
12
14
IPCMessageReader , IPCMessageWriter ,
13
15
createConnection , IConnection , TextDocumentSyncKind ,
14
16
TextDocument , Diagnostic , DiagnosticSeverity ,
15
17
InitializeParams , InitializeResult , TextDocumentPositionParams ,
16
- CompletionItem , CompletionItemKind , Definition , TextDocumentIdentifier ,
17
- Position , Range , TextEdit
18
+ CompletionItem , CompletionItemKind , Definition , Location , TextDocumentIdentifier ,
19
+ Position , Range , TextEdit , Hover
18
20
} from 'vscode-languageserver' ;
19
21
20
- import { TextDocuments , TextDocumentEvent } from './documents' ;
22
+ import { TextDocuments , TextDocumentEvent , fileNameToUri } from './documents' ;
21
23
import { ErrorCollector } from './errors' ;
22
24
23
25
import { Completion , Span } from '@angular/language-service' ;
@@ -60,7 +62,9 @@ connection.onInitialize((params): InitializeResult => {
60
62
completionProvider : {
61
63
resolveProvider : false ,
62
64
triggerCharacters : [ '<' , '.' , '*' , '[' , '(' ]
63
- }
65
+ } ,
66
+ definitionProvider : true ,
67
+ hoverProvider : true
64
68
}
65
69
}
66
70
} ) ;
@@ -93,7 +97,6 @@ function insertionToEdit(range: Range, insertText: string): TextEdit {
93
97
}
94
98
}
95
99
96
-
97
100
function getReplaceRange ( document : TextDocumentIdentifier , offset : number ) : Range {
98
101
const line = documents . getDocumentLine ( document , offset ) ;
99
102
if ( line && line . text && line . start <= offset && line . start + line . text . length >= offset ) {
@@ -146,5 +149,54 @@ connection.onCompletion((textDocumentPosition: TextDocumentPositionParams): Comp
146
149
}
147
150
} ) ;
148
151
152
+ function ngDefintionToDefintion ( definition : ng . Definition ) : Definition {
153
+ const locations = definition . map ( d => {
154
+ const document = TextDocumentIdentifier . create ( fileNameToUri ( d . fileName ) ) ;
155
+ const positions = documents . offsetsToPositions ( document , [ d . span . start , d . span . end ] ) ;
156
+ return { document, positions}
157
+ } ) . filter ( d => d . positions . length > 0 ) . map ( d => {
158
+ const range = Range . create ( d . positions [ 0 ] , d . positions [ 1 ] ) ;
159
+ return Location . create ( d . document . uri , range ) ;
160
+ } ) ;
161
+ if ( locations && locations . length ) {
162
+ return locations ;
163
+ }
164
+ }
165
+
166
+ connection . onDefinition ( ( textDocumentPosition : TextDocumentPositionParams ) : Definition => {
167
+ const { fileName, service, offset, languageId} = documents . getServiceInfo ( textDocumentPosition . textDocument ,
168
+ textDocumentPosition . position )
169
+ if ( fileName && service && offset != null ) {
170
+ let result = service . getDefinitionAt ( fileName , offset ) ;
171
+ if ( result ) {
172
+ return ngDefintionToDefintion ( result ) ;
173
+ }
174
+ }
175
+ } ) ;
176
+
177
+ function ngHoverToHover ( hover : ng . Hover , document : TextDocumentIdentifier ) : Hover {
178
+ if ( hover ) {
179
+ const positions = documents . offsetsToPositions ( document , [ hover . span . start , hover . span . end ] ) ;
180
+ if ( positions ) {
181
+ const range = Range . create ( positions [ 0 ] , positions [ 1 ] ) ;
182
+ return {
183
+ contents : { language : 'typescript' , value : hover . text . map ( t => t . text ) . join ( '' ) } ,
184
+ range
185
+ } ;
186
+ }
187
+ }
188
+ }
189
+
190
+ connection . onHover ( ( textDocumentPosition : TextDocumentPositionParams ) : Hover => {
191
+ const { fileName, service, offset, languageId} = documents . getServiceInfo ( textDocumentPosition . textDocument ,
192
+ textDocumentPosition . position )
193
+ if ( fileName && service && offset != null ) {
194
+ let result = service . getHoverAt ( fileName , offset ) ;
195
+ if ( result ) {
196
+ return ngHoverToHover ( result , textDocumentPosition . textDocument ) ;
197
+ }
198
+ }
199
+ } ) ;
200
+
149
201
// Listen on the connection
150
202
connection . listen ( ) ;
0 commit comments