88 SymbolInformation , SymbolKind , CompletionItem , Location , SignatureHelp , SignatureInformation , ParameterInformation ,
99 Definition , TextEdit , TextDocument , Diagnostic , DiagnosticSeverity , Range , CompletionItemKind , Hover , MarkedString ,
1010 DocumentHighlight , DocumentHighlightKind , CompletionList , Position , FormattingOptions , FoldingRange , FoldingRangeKind , SelectionRange ,
11- LanguageMode , Settings
11+ LanguageMode , Settings , SemanticTokenData
1212} from './languageModes' ;
1313import { getWordAtText , startsWith , isWhitespaceOnly , repeat } from '../utils/strings' ;
1414import { HTMLDocumentRegions } from './embeddedSupport' ;
@@ -17,18 +17,18 @@ import * as ts from 'typescript';
1717import { join } from 'path' ;
1818import { getSemanticTokens , getSemanticTokenLegend } from './javascriptSemanticTokens' ;
1919
20- const FILE_NAME = 'vscode://javascript/1' ; // the same 'file' is used for all contents
21- const TS_FILE_NAME = 'vscode://javascript/2.ts' ;
2220const JS_WORD_REGEX = / ( - ? \d * \. \d \w * ) | ( [ ^ \` \~ \! \@ \# \% \^ \& \* \( \) \- \= \+ \[ \{ \] \} \\ \| \; \: \' \" \, \. \< \> \/ \? \s ] + ) / g;
2321
2422let jquery_d_ts = join ( __dirname , '../lib/jquery.d.ts' ) ; // when packaged
2523if ( ! ts . sys . fileExists ( jquery_d_ts ) ) {
2624 jquery_d_ts = join ( __dirname , '../../lib/jquery.d.ts' ) ; // from source
2725}
2826
29- export function getJavaScriptMode ( documentRegions : LanguageModelCache < HTMLDocumentRegions > ) : LanguageMode {
27+ export function getJavaScriptMode ( documentRegions : LanguageModelCache < HTMLDocumentRegions > , id : 'javascript' | 'typescript' ) : LanguageMode {
3028 let jsDocuments = getLanguageModelCache < TextDocument > ( 10 , 60 , document => documentRegions . get ( document ) . getEmbeddedDocument ( 'javascript' ) ) ;
3129
30+ const workingFile = id === 'javascript' ? 'vscode://javascript/1.js' : 'vscode://javascript/2.ts' ; // the same 'file' is used for all contents
31+
3232 let compilerOptions : ts . CompilerOptions = { allowNonTsExtensions : true , allowJs : true , lib : [ 'lib.es6.d.ts' ] , target : ts . ScriptTarget . Latest , moduleResolution : ts . ModuleResolutionKind . Classic } ;
3333 let currentTextDocument : TextDocument ;
3434 let scriptFileVersion : number = 0 ;
@@ -40,18 +40,18 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
4040 }
4141 const host : ts . LanguageServiceHost = {
4242 getCompilationSettings : ( ) => compilerOptions ,
43- getScriptFileNames : ( ) => [ FILE_NAME , TS_FILE_NAME , jquery_d_ts ] ,
44- getScriptKind : ( fileName ) => fileName === TS_FILE_NAME ? ts . ScriptKind . TS : ts . ScriptKind . JS ,
43+ getScriptFileNames : ( ) => [ workingFile , jquery_d_ts ] ,
44+ getScriptKind : ( fileName ) => fileName . substr ( fileName . length - 2 ) === 'ts' ? ts . ScriptKind . TS : ts . ScriptKind . JS ,
4545 getScriptVersion : ( fileName : string ) => {
46- if ( fileName === FILE_NAME || fileName === TS_FILE_NAME ) {
46+ if ( fileName === workingFile ) {
4747 return String ( scriptFileVersion ) ;
4848 }
4949 return '1' ; // default lib an jquery.d.ts are static
5050 } ,
5151 getScriptSnapshot : ( fileName : string ) => {
5252 let text = '' ;
5353 if ( startsWith ( fileName , 'vscode:' ) ) {
54- if ( fileName === FILE_NAME || fileName === TS_FILE_NAME ) {
54+ if ( fileName === workingFile ) {
5555 text = currentTextDocument . getText ( ) ;
5656 }
5757 } else {
@@ -72,12 +72,12 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
7272
7373 return {
7474 getId ( ) {
75- return 'javascript' ;
75+ return id ;
7676 } ,
7777 doValidation ( document : TextDocument ) : Diagnostic [ ] {
7878 updateCurrentTextDocument ( document ) ;
79- const syntaxDiagnostics : ts . Diagnostic [ ] = jsLanguageService . getSyntacticDiagnostics ( FILE_NAME ) ;
80- const semanticDiagnostics = jsLanguageService . getSemanticDiagnostics ( FILE_NAME ) ;
79+ const syntaxDiagnostics : ts . Diagnostic [ ] = jsLanguageService . getSyntacticDiagnostics ( workingFile ) ;
80+ const semanticDiagnostics = jsLanguageService . getSemanticDiagnostics ( workingFile ) ;
8181 return syntaxDiagnostics . concat ( semanticDiagnostics ) . map ( ( diag : ts . Diagnostic ) : Diagnostic => {
8282 return {
8383 range : convertRange ( currentTextDocument , diag ) ,
@@ -90,7 +90,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
9090 doComplete ( document : TextDocument , position : Position ) : CompletionList {
9191 updateCurrentTextDocument ( document ) ;
9292 let offset = currentTextDocument . offsetAt ( position ) ;
93- let completions = jsLanguageService . getCompletionsAtPosition ( FILE_NAME , offset , { includeExternalModuleExports : false , includeInsertTextCompletions : false } ) ;
93+ let completions = jsLanguageService . getCompletionsAtPosition ( workingFile , offset , { includeExternalModuleExports : false , includeInsertTextCompletions : false } ) ;
9494 if ( ! completions ) {
9595 return { isIncomplete : false , items : [ ] } ;
9696 }
@@ -116,7 +116,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
116116 } ,
117117 doResolve ( document : TextDocument , item : CompletionItem ) : CompletionItem {
118118 updateCurrentTextDocument ( document ) ;
119- let details = jsLanguageService . getCompletionEntryDetails ( FILE_NAME , item . data . offset , item . label , undefined , undefined , undefined ) ;
119+ let details = jsLanguageService . getCompletionEntryDetails ( workingFile , item . data . offset , item . label , undefined , undefined , undefined ) ;
120120 if ( details ) {
121121 item . detail = ts . displayPartsToString ( details . displayParts ) ;
122122 item . documentation = ts . displayPartsToString ( details . documentation ) ;
@@ -126,7 +126,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
126126 } ,
127127 doHover ( document : TextDocument , position : Position ) : Hover | null {
128128 updateCurrentTextDocument ( document ) ;
129- let info = jsLanguageService . getQuickInfoAtPosition ( FILE_NAME , currentTextDocument . offsetAt ( position ) ) ;
129+ let info = jsLanguageService . getQuickInfoAtPosition ( workingFile , currentTextDocument . offsetAt ( position ) ) ;
130130 if ( info ) {
131131 let contents = ts . displayPartsToString ( info . displayParts ) ;
132132 return {
@@ -138,7 +138,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
138138 } ,
139139 doSignatureHelp ( document : TextDocument , position : Position ) : SignatureHelp | null {
140140 updateCurrentTextDocument ( document ) ;
141- let signHelp = jsLanguageService . getSignatureHelpItems ( FILE_NAME , currentTextDocument . offsetAt ( position ) , undefined ) ;
141+ let signHelp = jsLanguageService . getSignatureHelpItems ( workingFile , currentTextDocument . offsetAt ( position ) , undefined ) ;
142142 if ( signHelp ) {
143143 let ret : SignatureHelp = {
144144 activeSignature : signHelp . selectedItemIndex ,
@@ -175,7 +175,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
175175 } ,
176176 findDocumentHighlight ( document : TextDocument , position : Position ) : DocumentHighlight [ ] {
177177 updateCurrentTextDocument ( document ) ;
178- const highlights = jsLanguageService . getDocumentHighlights ( FILE_NAME , currentTextDocument . offsetAt ( position ) , [ FILE_NAME ] ) ;
178+ const highlights = jsLanguageService . getDocumentHighlights ( workingFile , currentTextDocument . offsetAt ( position ) , [ workingFile ] ) ;
179179 const out : DocumentHighlight [ ] = [ ] ;
180180 for ( const entry of highlights || [ ] ) {
181181 for ( const highlight of entry . highlightSpans ) {
@@ -189,7 +189,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
189189 } ,
190190 findDocumentSymbols ( document : TextDocument ) : SymbolInformation [ ] {
191191 updateCurrentTextDocument ( document ) ;
192- let items = jsLanguageService . getNavigationBarItems ( FILE_NAME ) ;
192+ let items = jsLanguageService . getNavigationBarItems ( workingFile ) ;
193193 if ( items ) {
194194 let result : SymbolInformation [ ] = [ ] ;
195195 let existing = Object . create ( null ) ;
@@ -225,9 +225,9 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
225225 } ,
226226 findDefinition ( document : TextDocument , position : Position ) : Definition | null {
227227 updateCurrentTextDocument ( document ) ;
228- let definition = jsLanguageService . getDefinitionAtPosition ( FILE_NAME , currentTextDocument . offsetAt ( position ) ) ;
228+ let definition = jsLanguageService . getDefinitionAtPosition ( workingFile , currentTextDocument . offsetAt ( position ) ) ;
229229 if ( definition ) {
230- return definition . filter ( d => d . fileName === FILE_NAME ) . map ( d => {
230+ return definition . filter ( d => d . fileName === workingFile ) . map ( d => {
231231 return {
232232 uri : document . uri ,
233233 range : convertRange ( currentTextDocument , d . textSpan )
@@ -238,9 +238,9 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
238238 } ,
239239 findReferences ( document : TextDocument , position : Position ) : Location [ ] {
240240 updateCurrentTextDocument ( document ) ;
241- let references = jsLanguageService . getReferencesAtPosition ( FILE_NAME , currentTextDocument . offsetAt ( position ) ) ;
241+ let references = jsLanguageService . getReferencesAtPosition ( workingFile , currentTextDocument . offsetAt ( position ) ) ;
242242 if ( references ) {
243- return references . filter ( d => d . fileName === FILE_NAME ) . map ( d => {
243+ return references . filter ( d => d . fileName === workingFile ) . map ( d => {
244244 return {
245245 uri : document . uri ,
246246 range : convertRange ( currentTextDocument , d . textSpan )
@@ -255,7 +255,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
255255 const parent = selectionRange . parent ? convertSelectionRange ( selectionRange . parent ) : undefined ;
256256 return SelectionRange . create ( convertRange ( currentTextDocument , selectionRange . textSpan ) , parent ) ;
257257 }
258- const range = jsLanguageService . getSmartSelectionRange ( FILE_NAME , currentTextDocument . offsetAt ( position ) ) ;
258+ const range = jsLanguageService . getSmartSelectionRange ( workingFile , currentTextDocument . offsetAt ( position ) ) ;
259259 return convertSelectionRange ( range ) ;
260260 } ,
261261 format ( document : TextDocument , range : Range , formatParams : FormattingOptions , settings : Settings = globalSettings ) : TextEdit [ ] {
@@ -273,7 +273,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
273273 end -= range . end . character ;
274274 lastLineRange = Range . create ( Position . create ( range . end . line , 0 ) , range . end ) ;
275275 }
276- let edits = jsLanguageService . getFormattingEditsForRange ( FILE_NAME , start , end , formatSettings ) ;
276+ let edits = jsLanguageService . getFormattingEditsForRange ( workingFile , start , end , formatSettings ) ;
277277 if ( edits ) {
278278 let result = [ ] ;
279279 for ( let edit of edits ) {
@@ -296,7 +296,7 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
296296 } ,
297297 getFoldingRanges ( document : TextDocument ) : FoldingRange [ ] {
298298 updateCurrentTextDocument ( document ) ;
299- let spans = jsLanguageService . getOutliningSpans ( FILE_NAME ) ;
299+ let spans = jsLanguageService . getOutliningSpans ( workingFile ) ;
300300 let ranges : FoldingRange [ ] = [ ] ;
301301 for ( let span of spans ) {
302302 let curr = convertRange ( currentTextDocument , span . textSpan ) ;
@@ -316,12 +316,9 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache<HTMLDocume
316316 onDocumentRemoved ( document : TextDocument ) {
317317 jsDocuments . onDocumentRemoved ( document ) ;
318318 } ,
319- getSemanticTokens ( document : TextDocument , ranges : Range [ ] | undefined ) : number [ ] {
319+ getSemanticTokens ( document : TextDocument ) : SemanticTokenData [ ] {
320320 updateCurrentTextDocument ( document ) ;
321- if ( ! ranges ) {
322- ranges = [ Range . create ( Position . create ( 0 , 0 ) , document . positionAt ( document . getText ( ) . length ) ) ] ;
323- }
324- return getSemanticTokens ( jsLanguageService , currentTextDocument , TS_FILE_NAME , ranges ) ;
321+ return getSemanticTokens ( jsLanguageService , currentTextDocument , workingFile ) ;
325322 } ,
326323 getSemanticTokenLegend ( ) : { types : string [ ] , modifiers : string [ ] } {
327324 return getSemanticTokenLegend ( ) ;
0 commit comments