Separate json and jsonc (fixes #3641, fixes #26830, fixes #19992)

This commit is contained in:
Martin Aeschlimann 2017-11-27 10:29:30 +01:00
parent 30296f6e61
commit 0c2de00043
5 changed files with 38 additions and 16 deletions

View file

@ -6,7 +6,7 @@
import * as path from 'path';
import { workspace, languages, ExtensionContext, extensions, Uri, TextDocument, ColorInformation, Color, ColorPresentation } from 'vscode';
import { workspace, languages, ExtensionContext, extensions, Uri, TextDocument, ColorInformation, Color, ColorPresentation, LanguageConfiguration } from 'vscode';
import { LanguageClient, LanguageClientOptions, RequestType, ServerOptions, TransportKind, NotificationType, DidChangeConfigurationNotification } from 'vscode-languageclient';
import TelemetryReporter from 'vscode-extension-telemetry';
import { ConfigurationFeature } from 'vscode-languageclient/lib/configuration.proposed';
@ -67,7 +67,7 @@ export function activate(context: ExtensionContext) {
// The server is implemented in node
let serverModule = context.asAbsolutePath(path.join('server', 'out', 'jsonServerMain.js'));
// The debug options for the server
let debugOptions = { execArgv: ['--nolazy', '--inspect=6004'] };
let debugOptions = { execArgv: ['--nolazy', '--inspect'] };
// If the extension is launch in debug mode the debug server options are use
// Otherwise the run options are used
@ -76,7 +76,7 @@ export function activate(context: ExtensionContext) {
debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions }
};
let documentSelector = ['json'];
let documentSelector = ['json', 'jsonc'];
// Options to control the language client
let clientOptions: LanguageClientOptions = {
@ -159,13 +159,15 @@ export function activate(context: ExtensionContext) {
}));
});
languages.setLanguageConfiguration('json', {
let languageConfiguration: LanguageConfiguration = {
wordPattern: /("(?:[^\\\"]*(?:\\.)?)*"?)|[^\s{}\[\],:]+/,
indentationRules: {
increaseIndentPattern: /^.*(\{[^}]*|\[[^\]]*)$/,
decreaseIndentPattern: /^\s*[}\]],?\s*$/
}
});
};
languages.setLanguageConfiguration('json', languageConfiguration);
languages.setLanguageConfiguration('jsonc', languageConfiguration);
}
function getSchemaAssociation(context: ExtensionContext): ISchemaAssociations {

View file

@ -7,7 +7,7 @@
"vscode": "0.10.x"
},
"activationEvents": [
"onLanguage:json"
"onLanguage:json", "onLanguage:jsonc"
],
"enableProposedApi": true,
"main": "./client/out/jsonMain",
@ -44,6 +44,19 @@
"application/manifest+json"
],
"configuration": "./language-configuration.json"
},
{
"id": "jsonc",
"aliases": [
"JSON with comments"
],
"filenames": [
"settings.json",
"launch.json",
"debug.json",
"keybindings.json"
],
"configuration": "./language-configuration.json"
}
],
"grammars": [
@ -51,6 +64,11 @@
"language": "json",
"scopeName": "source.json",
"path": "./syntaxes/JSON.tmLanguage.json"
},
{
"language": "jsonc",
"scopeName": "source.json",
"path": "./syntaxes/JSON.tmLanguage.json"
}
],
"jsonValidation": [

View file

@ -10,7 +10,7 @@
"dependencies": {
"jsonc-parser": "^1.0.0",
"request-light": "^0.2.1",
"vscode-json-languageservice": "^3.0.1",
"vscode-json-languageservice": "^3.0.2",
"vscode-languageserver": "^3.5.0",
"vscode-nls": "^2.0.2",
"vscode-uri": "^1.0.1"
@ -21,9 +21,9 @@
"scripts": {
"compile": "gulp compile-extension:json-server",
"watch": "gulp watch-extension:json-server",
"install-service-next": "npm install vscode-json-languageservice@next -f -S",
"install-service-local": "npm install ../../../../vscode-json-languageservice -f -S",
"install-server-next": "npm install vscode-languageserver@next -f -S",
"install-server-local": "npm install ../../../../vscode-languageserver-node/server -f -S"
"install-service-next": "yarn add vscode-json-languageservice@next",
"install-service-local": "npm install ../../../../vscode-json-languageservice -f",
"install-server-next": "yarn add vscode-languageserver@next",
"install-server-local": "npm install ../../../../vscode-languageserver-node/server -f"
}
}

View file

@ -17,7 +17,7 @@ import fs = require('fs');
import URI from 'vscode-uri';
import * as URL from 'url';
import Strings = require('./utils/strings');
import { JSONDocument, JSONSchema, LanguageSettings, getLanguageService } from 'vscode-json-languageservice';
import { JSONDocument, JSONSchema, LanguageSettings, getLanguageService, DocumentLanguageSettings } from 'vscode-json-languageservice';
import { getLanguageModelCache } from './languageModelCache';
import * as nls from 'vscode-nls';
@ -253,7 +253,9 @@ function validateTextDocument(textDocument: TextDocument): void {
}
let jsonDocument = getJSONDocument(textDocument);
languageService.doValidation(textDocument, jsonDocument).then(diagnostics => {
let documentSettings: DocumentLanguageSettings = textDocument.languageId === 'jsonc' ? { comments: 'ignore', trailingCommas: 'ignore' } : { comments: 'error', trailingCommas: 'error' };
languageService.doValidation(textDocument, jsonDocument, documentSettings).then(diagnostics => {
// Send the computed diagnostics to VSCode.
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
});

View file

@ -52,9 +52,9 @@ request-light@^0.2.1:
https-proxy-agent "^0.3.5"
vscode-nls "^2.0.2"
vscode-json-languageservice@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/vscode-json-languageservice/-/vscode-json-languageservice-3.0.1.tgz#e29eef0ef7c4548be3961b831d9b326cca9bd88d"
vscode-json-languageservice@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/vscode-json-languageservice/-/vscode-json-languageservice-3.0.2.tgz#66762127f24d32709ea5f410c58080ed8059d1e2"
dependencies:
jsonc-parser "^1.0.0"
vscode-languageserver-types "^3.5.0"