import { getCSSLanguageService } from 'vscode-css-languageservice' ; import { ClientCapabilities , DocumentContext , getLanguageService as getHTMLLanguageService , IHTMLDataProvider , SelectionRange } from 'vscode-html-languageservice' ; import { Color , ColorInformation , ColorPresentation , WorkspaceFolder } from 'vscode-languageserver' ; import { CompletionItem , CompletionList , Definition , Diagnostic , DocumentHighlight , DocumentLink , FoldingRange , FormattingOptions , Hover , Location , Position , Range , SignatureHelp , SymbolInformation , TextDocument , TextEdit } from 'vscode-languageserver-types' ; import { getLanguageModelCache , LanguageModelCache } from '../languageModelCache' ; import { getCSSMode } from './cssMode' ; import { getDocumentRegions , HTMLDocumentRegions } from './embeddedSupport' ; import { getHTMLMode } from './htmlMode' ; import { getJavaScriptMode } from './javascriptMode' ; export { ColorInformation , ColorPresentation , Color } ; export interface Settings { css ? : any ; html ? : any ; javascript ? : any ; } export interface Workspace { readonly settings : Settings ; readonly folders : WorkspaceFolder [ ] ; } export interface LanguageMode { getId ( ) : string ; getSelectionRanges ? : ( document : TextDocument , positions : Position [ ] ) => SelectionRange [ ] ; doValidation ? : ( document : TextDocument , settings ? : Settings ) => Diagnostic [ ] ; doComplete ? : ( document : TextDocument , position : Position , settings ? : Settings ) => CompletionList ; doResolve ? : ( document : TextDocument , item : CompletionItem ) => CompletionItem ; doHover ? : ( document : TextDocument , position : Position ) => Hover | null ; doSignatureHelp ? : ( document : TextDocument , position : Position ) => SignatureHelp | null ; findDocumentHighlight ? : ( document : TextDocument , position : Position ) => DocumentHighlight [ ] ; findDocumentSymbols ? : ( document : TextDocument ) => SymbolInformation [ ] ; findDocumentLinks ? : ( document : TextDocument , documentContext : DocumentContext ) => DocumentLink [ ] ; findDefinition ? : ( document : TextDocument , position : Position ) => Definition | null ; findReferences ? : ( document : TextDocument , position : Position ) => Location [ ] ; format ? : ( document : TextDocument , range : Range , options : FormattingOptions , settings ? : Settings ) => TextEdit [ ] ; findDocumentColors ? : ( document : TextDocument ) => ColorInformation [ ] ; getColorPresentations ? : ( document : TextDocument , color : Color , range : Range ) => ColorPresentation [ ] ; doAutoClose ? : ( document : TextDocument , position : Position ) => string | null ; getFoldingRanges ? : ( document : TextDocument ) => FoldingRange [ ] ; onDocumentRemoved ( document ) : void ; dispose ( ) : void ; } export interface LanguageModes { getModeAtPosition ( document , position ) : LanguageMode | undefined ; getModesInRange ( document , range ) : LanguageModeRange [ ] ; getAllModes ( ) : LanguageMode [ ] ; getAllModesInDocument ( document ) : LanguageMode [ ] ; getMode ( languageId ) : LanguageMode | undefined ; onDocumentRemoved ( document ) : void ; dispose ( ) : void ; } export interface LanguageModeRange extends Range { mode : LanguageMode | undefined ; attributeValue ? : boolean ; } export function getLanguageModes ( supportedLanguages , workspace , clientCapabilities , customDataProviders ? ) { const htmlLanguageService = getHTMLLanguageService ( { customDataProviders , clientCapabilities } ) ; const cssLanguageService = getCSSLanguageService ( { clientCapabilities } ) ; let documentRegions = getLanguageModelCache < HTMLDocumentRegions > ( 10 , 60 , document => getDocumentRegions ( htmlLanguageService , document ) ) ; let modelCaches = [ ] ; modelCaches . push ( documentRegions ) ; let modes = Object . create ( null ) ; modes [ 'html' ] = getHTMLMode ( htmlLanguageService , workspace ) ; if ( supportedLanguages [ 'css' ] ) { modes [ 'css' ] = getCSSMode ( cssLanguageService , documentRegions , workspace ) ; } if ( supportedLanguages [ 'javascript' ] ) { modes [ 'javascript' ] = getJavaScriptMode ( documentRegions ) ; } return { getModeAtPosition ( document , position ) { let languageId = documentRegions . get ( document ) . getLanguageAtPosition ( position ) ; if ( languageId ) { return modes [ languageId ] ; } return undefined ; } , getModesInRange ( document , range ) { return documentRegions . get ( document ) . getLanguageRanges ( range ) . map ( r => { return < LanguageModeRange > { start : r . start , end : r . end , mode : r . languageId && modes [ r . languageId ] , attributeValue : r . attributeValue } ; } ) ; } , getAllModesInDocument ( document ) { let result = [ ] ; for ( let languageId of documentRegions . get ( document ) . getLanguagesInDocument ( ) ) { let mode = modes [ languageId ] ; if ( mode ) { result . push ( mode ) ; } } return result ; } , getAllModes ( ) { let result = [ ] ; for ( let languageId in modes ) { let mode = modes [ languageId ] ; if ( mode ) { result . push ( mode ) ; } } return result ; } , getMode ( languageId ) { return modes [ languageId ] ; } , onDocumentRemoved ( document ) { modelCaches . forEach ( mc => mc . onDocumentRemoved ( document ) ) ; for ( let mode in modes ) { modes [ mode ] . onDocumentRemoved ( document ) ; } } , dispose ( ) { modelCaches . forEach ( mc => mc . dispose ( ) ) ; modelCaches = [ ] ; for ( let mode in modes ) { modes [ mode ] . dispose ( ) ; } modes = { } ; } } ; }