55'use strict' ;
66
77import { createConnection , IConnection , TextDocuments , InitializeParams , InitializeResult , RequestType , DocumentRangeFormattingRequest , Disposable , DocumentSelector , TextDocumentPositionParams , ServerCapabilities , Position } from 'vscode-languageserver' ;
8- import { DocumentContext } from 'vscode-html-languageservice' ;
98import { TextDocument , Diagnostic , DocumentLink , SymbolInformation } from 'vscode-languageserver-types' ;
109import { getLanguageModes , LanguageModes , Settings } from './modes/languageModes' ;
1110
@@ -15,13 +14,11 @@ import { DidChangeWorkspaceFoldersNotification, WorkspaceFolder } from 'vscode-l
1514
1615import { format } from './modes/formatting' ;
1716import { pushAll } from './utils/arrays' ;
18- import { endsWith , startsWith } from './utils/strings' ;
19-
20- import * as url from 'url' ;
21- import * as path from 'path' ;
17+ import { getDocumentContext } from './utils/documentContext' ;
2218import uri from 'vscode-uri' ;
2319
2420import * as nls from 'vscode-nls' ;
21+
2522nls . config ( process . env [ 'VSCODE_NLS_CONFIG' ] ) ;
2623
2724namespace TagCloseRequest {
@@ -41,7 +38,6 @@ let documents: TextDocuments = new TextDocuments();
4138// for open, change and close text document events
4239documents . listen ( connection ) ;
4340
44- let workspacePath : string | undefined | null ;
4541let workspaceFolders : WorkspaceFolder [ ] | undefined ;
4642
4743var languageModes : LanguageModes ;
@@ -77,8 +73,13 @@ function getDocumentSettings(textDocument: TextDocument, needsDocumentSettings:
7773connection . onInitialize ( ( params : InitializeParams ) : InitializeResult => {
7874 let initializationOptions = params . initializationOptions ;
7975
80- workspacePath = params . rootPath ;
8176 workspaceFolders = ( < any > params ) . workspaceFolders ;
77+ if ( ! Array . isArray ( workspaceFolders ) ) {
78+ workspaceFolders = [ ] ;
79+ if ( params . rootPath ) {
80+ workspaceFolders . push ( { name : '' , uri : uri . file ( params . rootPath ) . toString ( ) } ) ;
81+ }
82+ }
8283
8384 languageModes = getLanguageModes ( initializationOptions ? initializationOptions . embeddedLanguages : { css : true , javascript : true } ) ;
8485 documents . onDidClose ( e => {
@@ -305,45 +306,19 @@ connection.onDocumentRangeFormatting(async formatParams => {
305306
306307connection . onDocumentLinks ( documentLinkParam => {
307308 let document = documents . get ( documentLinkParam . textDocument . uri ) ;
308- let documentContext : DocumentContext = {
309- resolveReference : ( ref , base ) => {
310- if ( base ) {
311- ref = url . resolve ( base , ref ) ;
312- }
313- if ( ref [ 0 ] === '/' ) {
314- let root = getRootFolder ( document . uri ) ;
315- if ( root ) {
316- return uri . file ( path . join ( root , ref ) ) . toString ( ) ;
317- }
318- }
319- return url . resolve ( document . uri , ref ) ;
320- } ,
321-
322- } ;
323309 let links : DocumentLink [ ] = [ ] ;
324- languageModes . getAllModesInDocument ( document ) . forEach ( m => {
325- if ( m . findDocumentLinks ) {
326- pushAll ( links , m . findDocumentLinks ( document , documentContext ) ) ;
327- }
328- } ) ;
310+ if ( document ) {
311+ let documentContext = getDocumentContext ( document . uri , workspaceFolders ) ;
312+ languageModes . getAllModesInDocument ( document ) . forEach ( m => {
313+ if ( m . findDocumentLinks ) {
314+ pushAll ( links , m . findDocumentLinks ( document , documentContext ) ) ;
315+ }
316+ } ) ;
317+ }
329318 return links ;
330319} ) ;
331320
332- function getRootFolder ( docUri : string ) : string | undefined {
333- if ( workspaceFolders ) {
334- for ( let folder of workspaceFolders ) {
335- let folderURI = folder . uri ;
336- if ( ! endsWith ( folderURI , '/' ) ) {
337- folderURI = folderURI + '/' ;
338- }
339- if ( startsWith ( docUri , folderURI ) ) {
340- return uri . parse ( folderURI ) . fsPath ;
341- }
342- }
343- return void 0 ;
344- }
345- return workspacePath ;
346- }
321+
347322
348323connection . onDocumentSymbol ( documentSymbolParms => {
349324 let document = documents . get ( documentSymbolParms . textDocument . uri ) ;
0 commit comments