import { JSONCompletion } from './services/jsonCompletion' ; import { JSONHover } from './services/jsonHover' ; import { JSONValidation } from './services/jsonValidation' ; import { JSONDocumentSymbols } from './services/jsonDocumentSymbols' ; import { parse as parseJSON , JSONDocument as InternalJSONDocument , newJSONDocument } from './parser/jsonParser' ; import { schemaContributions } from './services/configuration' ; import { JSONSchemaService } from './services/jsonSchemaService' ; import { getFoldingRanges } from './services/jsonFolding' ; import { getSelectionRanges } from './services/jsonSelectionRanges' ; import { format as formatJSON , Range as JSONCRange } from 'jsonc-parser' ; import { Thenable , ASTNode , Color , ColorInformation , ColorPresentation , LanguageServiceParams , LanguageSettings , DocumentLanguageSettings , FoldingRange , JSONSchema , SelectionRange , FoldingRangesContext , DocumentSymbolsContext , ColorInformationContext as DocumentColorsContext , TextDocument , Position , CompletionItem , CompletionList , Hover , Range , SymbolInformation , Diagnostic , TextEdit , FormattingOptions , DocumentSymbol , DefinitionLink , MatchingSchema , JSONLanguageStatus } from './jsonLanguageTypes' ; import { findLinks } from './services/jsonLinks' ; import { DocumentLink } from 'vscode-languageserver-types' ; export type JSONDocument = { root : ASTNode | undefined ; getNodeFromOffset ( offset , includeRightBound ? ) : ASTNode | undefined ; } ; export * from './jsonLanguageTypes' ; export interface LanguageService { configure ( settings ) : void ; doValidation ( document , jsonDocument , documentSettings ? , schema ? ) : Thenable < Diagnostic [ ] > ; parseJSONDocument ( document ) : JSONDocument ; newJSONDocument ( rootNode , syntaxDiagnostics ? ) : JSONDocument ; resetSchema ( uri ) : boolean ; getMatchingSchemas ( document , jsonDocument , schema ? ) : Thenable < MatchingSchema [ ] > ; getLanguageStatus ( document , jsonDocument ) : JSONLanguageStatus ; doResolve ( item ) : Thenable < CompletionItem > ; doComplete ( document , position , doc ) : Thenable < CompletionList | null > ; findDocumentSymbols ( document , doc , context ? ) : SymbolInformation [ ] ; findDocumentSymbols2 ( document , doc , context ? ) : DocumentSymbol [ ] ; findDocumentColors ( document , doc , context ? ) : Thenable < ColorInformation [ ] > ; getColorPresentations ( document , doc , color , range ) : ColorPresentation [ ] ; doHover ( document , position , doc ) : Thenable < Hover | null > ; format ( document , range , options ) : TextEdit [ ] ; getFoldingRanges ( document , context ? ) : FoldingRange [ ] ; getSelectionRanges ( document , positions , doc ) : SelectionRange [ ] ; findDefinition ( document , position , doc ) : Thenable < DefinitionLink [ ] > ; findLinks ( document , doc ) : Thenable < DocumentLink [ ] > ; } export function getLanguageService ( params ) { const promise = params . promiseConstructor || Promise ; const jsonSchemaService = new JSONSchemaService ( params . schemaRequestService , params . workspaceContext , promise ) ; jsonSchemaService . setSchemaContributions ( schemaContributions ) ; const jsonCompletion = new JSONCompletion ( jsonSchemaService , params . contributions , promise , params . clientCapabilities ) ; const jsonHover = new JSONHover ( jsonSchemaService , params . contributions , promise ) ; const jsonDocumentSymbols = new JSONDocumentSymbols ( jsonSchemaService ) ; const jsonValidation = new JSONValidation ( jsonSchemaService , promise ) ; return { configure : ( settings ) => { jsonSchemaService . clearExternalSchemas ( ) ; if ( settings . schemas ) { settings . schemas . forEach ( settings => { jsonSchemaService . registerExternalSchema ( settings . uri , settings . fileMatch , settings . schema ) ; } ) ; } jsonValidation . configure ( settings ) ; } , resetSchema : ( uri ) => jsonSchemaService . onResourceChange ( uri ) , doValidation : jsonValidation . doValidation . bind ( jsonValidation ) , getLanguageStatus : jsonValidation . getLanguageStatus . bind ( jsonValidation ) , parseJSONDocument : ( document ) => parseJSON ( document , { collectComments : true } ) , newJSONDocument : ( root , diagnostics ) => newJSONDocument ( root , diagnostics ) , getMatchingSchemas : jsonSchemaService . getMatchingSchemas . bind ( jsonSchemaService ) , doResolve : jsonCompletion . doResolve . bind ( jsonCompletion ) , doComplete : jsonCompletion . doComplete . bind ( jsonCompletion ) , findDocumentSymbols : jsonDocumentSymbols . findDocumentSymbols . bind ( jsonDocumentSymbols ) , findDocumentSymbols2 : jsonDocumentSymbols . findDocumentSymbols2 . bind ( jsonDocumentSymbols ) , findDocumentColors : jsonDocumentSymbols . findDocumentColors . bind ( jsonDocumentSymbols ) , getColorPresentations : jsonDocumentSymbols . getColorPresentations . bind ( jsonDocumentSymbols ) , doHover : jsonHover . doHover . bind ( jsonHover ) , getFoldingRanges , getSelectionRanges , findDefinition : ( ) => Promise . resolve ( [ ] ) , findLinks , format : ( d , r , o ) => { let range = undefined ; if ( r ) { const offset = d . offsetAt ( r . start ) ; const length = d . offsetAt ( r . end ) - offset ; range = { offset , length } ; } const options = { tabSize : o ? o . tabSize : 4 , insertSpaces : o ?. insertSpaces === true , insertFinalNewline : o ?. insertFinalNewline === true , eol : '\n' } ; return formatJSON ( d . getText ( ) , range , options ) . map ( e => { return TextEdit . replace ( Range . create ( d . positionAt ( e . offset ) , d . positionAt ( e . offset + e . length ) ) , e . content ) ; } ) ; } } ; }