@@ -18,15 +18,19 @@ import 'monaco-editor/esm/vs/editor/standalone/browser/referenceSearch/standalon
1818import 'monaco-editor/esm/vs/editor/standalone/browser/toggleHighContrast/toggleHighContrast.js' ;
1919
2020import * as monaco from 'monaco-editor/esm/vs/editor/editor.api' ;
21+ import * as vscode from 'vscode'
2122
2223import { buildWorkerDefinition } from 'monaco-editor-workers' ;
2324buildWorkerDefinition ( 'dist' , new URL ( '' , window . location . href ) . href , false ) ;
2425
2526import { getLanguageService , TextDocument } from "vscode-json-languageservice" ;
26- import { MonacoToProtocolConverter , ProtocolToMonacoConverter } from 'monaco-languageclient' ;
27+ import { createConverter as createCodeConverter } from "vscode-languageclient/lib/common/codeConverter"
28+ import { createConverter as createProtocolConverter } from "vscode-languageclient/lib/common/protocolConverter"
29+ const codeConverter = createCodeConverter ( )
30+ const protocolConverter = createProtocolConverter ( undefined , true , true )
2731
2832const LANGUAGE_ID = 'json' ;
29- const MODEL_URI = 'inmemory://model .json'
33+ const MODEL_URI = 'inmemory://vscodeDocument .json'
3034const MONACO_URI = monaco . Uri . parse ( MODEL_URI ) ;
3135
3236// register the JSON language with Monaco
@@ -42,20 +46,19 @@ const value = `{
4246 "$schema": "http://json.schemastore.org/coffeelint",
4347 "line_endings": "unix"
4448}` ;
49+ const model = monaco . editor . createModel ( value , LANGUAGE_ID , MONACO_URI )
4550monaco . editor . create ( document . getElementById ( "container" ) ! , {
46- model : monaco . editor . createModel ( value , LANGUAGE_ID , MONACO_URI ) ,
51+ model,
4752 glyphMargin : true ,
4853 lightbulb : {
4954 enabled : true
5055 }
5156} ) ;
5257
53- function getModel ( ) : monaco . editor . IModel {
54- return monaco . editor . getModel ( MONACO_URI ) as monaco . editor . IModel ;
55- }
58+ const vscodeDocument = vscode . workspace . textDocuments [ 0 ]
5659
57- function createDocument ( model : monaco . editor . IReadOnlyModel ) {
58- return TextDocument . create ( MODEL_URI , model . getLanguageId ( ) , model . getVersionId ( ) , model . getValue ( ) ) ;
60+ function createDocument ( vscodeDocument : vscode . TextDocument ) {
61+ return TextDocument . create ( MODEL_URI , vscodeDocument . languageId , vscodeDocument . version , vscodeDocument . getText ( ) ) ;
5962}
6063
6164function resolveSchema ( url : string ) : Promise < string > {
@@ -69,61 +72,60 @@ function resolveSchema(url: string): Promise<string> {
6972 return promise ;
7073}
7174
72- const m2p = new MonacoToProtocolConverter ( monaco ) ;
73- const p2m = new ProtocolToMonacoConverter ( monaco ) ;
75+ // const codeConverter = new MonacoToProtocolConverter(monaco);
76+ // const protocolConverter = new ProtocolToMonacoConverter(monaco);
7477const jsonService = getLanguageService ( {
7578 schemaRequestService : resolveSchema
7679} ) ;
7780const pendingValidationRequests = new Map < string , NodeJS . Timeout > ( ) ;
7881
79- monaco . languages . registerCompletionItemProvider ( LANGUAGE_ID , {
80- provideCompletionItems ( model , position , _context , _token ) : monaco . Thenable < monaco . languages . CompletionList > {
81- const document = createDocument ( model ) ;
82- const wordUntil = model . getWordUntilPosition ( position ) ;
83- const defaultRange = new monaco . Range ( position . lineNumber , wordUntil . startColumn , position . lineNumber , wordUntil . endColumn ) ;
82+ vscode . languages . registerCompletionItemProvider ( LANGUAGE_ID , {
83+ async provideCompletionItems ( vscodeDocument , position , _token , _context ) {
84+ const document = createDocument ( vscodeDocument )
8485 const jsonDocument = jsonService . parseJSONDocument ( document ) ;
85- return jsonService . doComplete ( document , m2p . asPosition ( position . lineNumber , position . column ) , jsonDocument ) . then ( ( list ) => {
86- return p2m . asCompletionResult ( list , defaultRange ) ;
87- } ) ;
86+ const completionList = await jsonService . doComplete ( document , codeConverter . asPosition ( position ) , jsonDocument ) ;
87+ return protocolConverter . asCompletionResult ( completionList ) ;
8888 } ,
8989
90- resolveCompletionItem ( item , _token ) : monaco . languages . CompletionItem | monaco . Thenable < monaco . languages . CompletionItem > {
91- return jsonService . doResolve ( m2p . asCompletionItem ( item ) ) . then ( result => p2m . asCompletionItem ( result , item . range ) ) ;
90+ resolveCompletionItem ( item , _token ) {
91+ return jsonService . doResolve ( codeConverter . asCompletionItem ( item ) ) . then ( result => protocolConverter . asCompletionItem ( result ) ) ;
9292 }
9393} ) ;
9494
95- monaco . languages . registerDocumentRangeFormattingEditProvider ( LANGUAGE_ID , {
96- provideDocumentRangeFormattingEdits ( model , range , options , _token ) : monaco . languages . TextEdit [ ] | monaco . Thenable < monaco . languages . TextEdit [ ] > {
97- const document = createDocument ( model ) ;
98- const edits = jsonService . format ( document , m2p . asRange ( range ) , m2p . asFormattingOptions ( options ) ) ;
99- return p2m . asTextEdits ( edits ) ;
95+ vscode . languages . registerDocumentRangeFormattingEditProvider ( LANGUAGE_ID , {
96+ provideDocumentRangeFormattingEdits ( vscodeDocument , range , options , _token ) {
97+ const document = createDocument ( vscodeDocument ) ;
98+ const edits = jsonService . format ( document , codeConverter . asRange ( range ) , codeConverter . asFormattingOptions ( options , { } ) ) ;
99+ return protocolConverter . asTextEdits ( edits ) ;
100100 }
101101} ) ;
102102
103- monaco . languages . registerDocumentSymbolProvider ( LANGUAGE_ID , {
104- provideDocumentSymbols ( model , _token ) : monaco . languages . DocumentSymbol [ ] | monaco . Thenable < monaco . languages . DocumentSymbol [ ] > {
105- const document = createDocument ( model ) ;
103+ vscode . languages . registerDocumentSymbolProvider ( LANGUAGE_ID , {
104+ provideDocumentSymbols ( vscodeDocument , _token ) {
105+ const document = createDocument ( vscodeDocument ) ;
106106 const jsonDocument = jsonService . parseJSONDocument ( document ) ;
107- return p2m . asSymbolInformations ( jsonService . findDocumentSymbols ( document , jsonDocument ) ) ;
107+ return protocolConverter . asSymbolInformations ( jsonService . findDocumentSymbols ( document , jsonDocument ) ) ;
108108 }
109109} ) ;
110110
111- monaco . languages . registerHoverProvider ( LANGUAGE_ID , {
112- provideHover ( model , position , _token ) : monaco . languages . Hover | monaco . Thenable < monaco . languages . Hover > {
113- const document = createDocument ( model ) ;
111+ vscode . languages . registerHoverProvider ( LANGUAGE_ID , {
112+ provideHover ( vscodeDocument , position , _token ) {
113+ const document = createDocument ( vscodeDocument ) ;
114114 const jsonDocument = jsonService . parseJSONDocument ( document ) ;
115- return jsonService . doHover ( document , m2p . asPosition ( position . lineNumber , position . column ) , jsonDocument ) . then ( ( hover ) => {
116- return p2m . asHover ( hover ) ! ;
115+ return jsonService . doHover ( document , codeConverter . asPosition ( position ) , jsonDocument ) . then ( ( hover ) => {
116+ return protocolConverter . asHover ( hover ) ! ;
117117 } ) ;
118118 }
119119} ) ;
120120
121- getModel ( ) . onDidChangeContent ( ( _event ) => {
121+ model . onDidChangeContent ( ( _event ) => {
122122 validate ( ) ;
123123} ) ;
124+ validate ( ) ;
124125
125126function validate ( ) : void {
126- const document = createDocument ( getModel ( ) ) ;
127+ console . log ( vscode . workspace . textDocuments . map ( d => d . uri . toString ( ) ) , MODEL_URI )
128+ const document = createDocument ( vscodeDocument ) ;
127129 cleanPendingValidation ( document ) ;
128130 pendingValidationRequests . set ( document . uri , setTimeout ( ( ) => {
129131 pendingValidationRequests . delete ( document . uri ) ;
@@ -139,18 +141,22 @@ function cleanPendingValidation(document: TextDocument): void {
139141 }
140142}
141143
144+ const diagnosticCollection = vscode . languages . createDiagnosticCollection ( 'json' ) ;
142145function doValidate ( document : TextDocument ) : void {
143146 if ( document . getText ( ) . length === 0 ) {
144147 cleanDiagnostics ( ) ;
145148 return ;
146149 }
147150 const jsonDocument = jsonService . parseJSONDocument ( document ) ;
148- jsonService . doValidation ( document , jsonDocument ) . then ( ( diagnostics ) => {
149- const markers = p2m . asDiagnostics ( diagnostics ) ;
150- monaco . editor . setModelMarkers ( getModel ( ) , 'default' , markers ) ;
151+
152+
153+ jsonService . doValidation ( document , jsonDocument ) . then ( async ( pDiagnostics ) => {
154+ const diagnostics = await protocolConverter . asDiagnostics ( pDiagnostics ) ;
155+ console . log ( diagnostics )
156+ diagnosticCollection . set ( MONACO_URI , diagnostics ) ;
151157 } ) ;
152158}
153159
154160function cleanDiagnostics ( ) : void {
155- monaco . editor . setModelMarkers ( getModel ( ) , 'default' , [ ] ) ;
161+ diagnosticCollection . clear ( )
156162}
0 commit comments