mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-01 14:01:38 +01:00
[json] use vscode-json-languageservice
This commit is contained in:
@@ -6,8 +6,7 @@
|
||||
|
||||
import {MarkedString, CompletionItemKind, CompletionItem} from 'vscode-languageserver';
|
||||
import Strings = require('../utils/strings');
|
||||
import {IJSONWorkerContribution, ISuggestionsCollector} from '../jsonContributions';
|
||||
import {JSONLocation} from '../jsonLocation';
|
||||
import {JSONWorkerContribution, JSONPath, CompletionsCollector} from 'vscode-json-languageservice';
|
||||
|
||||
import * as nls from 'vscode-nls';
|
||||
const localize = nls.loadMessageBundle();
|
||||
@@ -17,7 +16,7 @@ let globProperties: CompletionItem[] = [
|
||||
{ kind: CompletionItemKind.Value, label: localize('assocLabelPath', "Files with Path"), insertText: '"/{{path to file}}/*.{{extension}}": "{{language}}"', documentation: localize('assocDescriptionPath', "Map all files matching the absolute path glob pattern in their path to the language with the given identifier.") }
|
||||
];
|
||||
|
||||
export class FileAssociationContribution implements IJSONWorkerContribution {
|
||||
export class FileAssociationContribution implements JSONWorkerContribution {
|
||||
private languageIds:string[];
|
||||
|
||||
constructor() {
|
||||
@@ -31,20 +30,20 @@ export class FileAssociationContribution implements IJSONWorkerContribution {
|
||||
return Strings.endsWith(resource, '/settings.json');
|
||||
}
|
||||
|
||||
public collectDefaultSuggestions(resource: string, result: ISuggestionsCollector): Thenable<any> {
|
||||
public collectDefaultCompletions(resource: string, result: CompletionsCollector): Thenable<any> {
|
||||
return null;
|
||||
}
|
||||
|
||||
public collectPropertySuggestions(resource: string, location: JSONLocation, currentWord: string, addValue: boolean, isLast: boolean, result: ISuggestionsCollector): Thenable<any> {
|
||||
if (this.isSettingsFile(resource) && location.matches(['files.associations'])) {
|
||||
public collectPropertyCompletions(resource: string, location: JSONPath, currentWord: string, addValue: boolean, isLast: boolean, result: CompletionsCollector): Thenable<any> {
|
||||
if (this.isSettingsFile(resource) && location.length === 1 && location[0] === 'files.associations') {
|
||||
globProperties.forEach((e) => result.add(e));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public collectValueSuggestions(resource: string, location: JSONLocation, currentKey: string, result: ISuggestionsCollector): Thenable<any> {
|
||||
if (this.isSettingsFile(resource) && location.matches(['files.associations'])) {
|
||||
public collectValueCompletions(resource: string, location: JSONPath, currentKey: string, result: CompletionsCollector): Thenable<any> {
|
||||
if (this.isSettingsFile(resource) && location.length === 1 && location[0] === 'files.associations') {
|
||||
this.languageIds.forEach(l => {
|
||||
result.add({
|
||||
kind: CompletionItemKind.Value,
|
||||
@@ -57,7 +56,7 @@ export class FileAssociationContribution implements IJSONWorkerContribution {
|
||||
return null;
|
||||
}
|
||||
|
||||
public getInfoContribution(resource: string, location: JSONLocation): Thenable<MarkedString[]> {
|
||||
public getInfoContribution(resource: string, location: JSONPath): Thenable<MarkedString[]> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,7 @@
|
||||
|
||||
import {MarkedString, CompletionItemKind, CompletionItem} from 'vscode-languageserver';
|
||||
import Strings = require('../utils/strings');
|
||||
import {IJSONWorkerContribution, ISuggestionsCollector} from '../jsonContributions';
|
||||
import {JSONLocation} from '../jsonLocation';
|
||||
import {JSONWorkerContribution, JSONPath, CompletionsCollector} from 'vscode-json-languageservice';
|
||||
|
||||
import * as nls from 'vscode-nls';
|
||||
const localize = nls.loadMessageBundle();
|
||||
@@ -27,7 +26,7 @@ let globValues: CompletionItem[] = [
|
||||
{ kind: CompletionItemKind.Value, label: localize('derivedLabel', "Files with Siblings by Name"), insertText: '{ "when": "$(basename).{{extension}}" }', documentation: localize('siblingsDescription', "Match files that have siblings with the same name but a different extension.") }
|
||||
];
|
||||
|
||||
export class GlobPatternContribution implements IJSONWorkerContribution {
|
||||
export class GlobPatternContribution implements JSONWorkerContribution {
|
||||
|
||||
constructor() {
|
||||
}
|
||||
@@ -36,27 +35,27 @@ export class GlobPatternContribution implements IJSONWorkerContribution {
|
||||
return Strings.endsWith(resource, '/settings.json');
|
||||
}
|
||||
|
||||
public collectDefaultSuggestions(resource: string, result: ISuggestionsCollector): Thenable<any> {
|
||||
public collectDefaultCompletions(resource: string, result: CompletionsCollector): Thenable<any> {
|
||||
return null;
|
||||
}
|
||||
|
||||
public collectPropertySuggestions(resource: string, location: JSONLocation, currentWord: string, addValue: boolean, isLast: boolean, result: ISuggestionsCollector): Thenable<any> {
|
||||
if (this.isSettingsFile(resource) && (location.matches(['files.exclude']) || location.matches(['search.exclude']))) {
|
||||
public collectPropertyCompletions(resource: string, location: JSONPath, currentWord: string, addValue: boolean, isLast: boolean, result: CompletionsCollector): Thenable<any> {
|
||||
if (this.isSettingsFile(resource) && location.length === 1 && ((location[0] === 'files.exclude') || (location[0] === 'search.exclude'))) {
|
||||
globProperties.forEach((e) => result.add(e));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public collectValueSuggestions(resource: string, location: JSONLocation, currentKey: string, result: ISuggestionsCollector): Thenable<any> {
|
||||
if (this.isSettingsFile(resource) && (location.matches(['files.exclude']) || location.matches(['search.exclude']))) {
|
||||
public collectValueCompletions(resource: string, location: JSONPath, currentKey: string, result: CompletionsCollector): Thenable<any> {
|
||||
if (this.isSettingsFile(resource) && location.length === 1 && ((location[0] === 'files.exclude') || (location[0] === 'search.exclude'))) {
|
||||
globValues.forEach((e) => result.add(e));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public getInfoContribution(resource: string, location: JSONLocation): Thenable<MarkedString[]> {
|
||||
public getInfoContribution(resource: string, location: JSONPath): Thenable<MarkedString[]> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,8 @@
|
||||
import {MarkedString, CompletionItemKind, CompletionItem} from 'vscode-languageserver';
|
||||
import Strings = require('../utils/strings');
|
||||
import {XHRResponse, getErrorStatusDescription} from 'request-light';
|
||||
import {IJSONWorkerContribution, ISuggestionsCollector} from '../jsonContributions';
|
||||
import {RequestService} from '../jsonLanguageService';
|
||||
import {JSONLocation} from '../jsonLocation';
|
||||
import {JSONWorkerContribution, JSONPath, CompletionsCollector} from 'vscode-json-languageservice';
|
||||
import {xhr} from 'request-light';
|
||||
|
||||
import * as nls from 'vscode-nls';
|
||||
const localize = nls.loadMessageBundle();
|
||||
@@ -28,15 +27,13 @@ interface NugetServices {
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
export class ProjectJSONContribution implements IJSONWorkerContribution {
|
||||
export class ProjectJSONContribution implements JSONWorkerContribution {
|
||||
|
||||
private requestService : RequestService;
|
||||
private cachedProjects: { [id: string]: { version: string, description: string, time: number }} = {};
|
||||
private cacheSize: number = 0;
|
||||
private nugetIndexPromise: Thenable<NugetServices>;
|
||||
|
||||
public constructor(requestService: RequestService) {
|
||||
this.requestService = requestService;
|
||||
public constructor() {
|
||||
}
|
||||
|
||||
private isProjectJSONFile(resource: string): boolean {
|
||||
@@ -104,7 +101,7 @@ export class ProjectJSONContribution implements IJSONWorkerContribution {
|
||||
});
|
||||
}
|
||||
|
||||
public collectDefaultSuggestions(resource: string, result: ISuggestionsCollector): Thenable<any> {
|
||||
public collectDefaultCompletions(resource: string, result: CompletionsCollector): Thenable<any> {
|
||||
if (this.isProjectJSONFile(resource)) {
|
||||
let defaultValue = {
|
||||
'version': '{{1.0.0-*}}',
|
||||
@@ -120,7 +117,7 @@ export class ProjectJSONContribution implements IJSONWorkerContribution {
|
||||
}
|
||||
|
||||
private makeJSONRequest<T>(url: string) : Thenable<T> {
|
||||
return this.requestService({
|
||||
return xhr({
|
||||
url : url
|
||||
}).then(success => {
|
||||
if (success.status === 200) {
|
||||
@@ -136,8 +133,8 @@ export class ProjectJSONContribution implements IJSONWorkerContribution {
|
||||
});
|
||||
}
|
||||
|
||||
public collectPropertySuggestions(resource: string, location: JSONLocation, currentWord: string, addValue: boolean, isLast:boolean, result: ISuggestionsCollector) : Thenable<any> {
|
||||
if (this.isProjectJSONFile(resource) && (location.matches(['dependencies']) || location.matches(['frameworks', '*', 'dependencies']) || location.matches(['frameworks', '*', 'frameworkAssemblies']))) {
|
||||
public collectPropertyCompletions(resource: string, location: JSONPath, currentWord: string, addValue: boolean, isLast:boolean, result: CompletionsCollector) : Thenable<any> {
|
||||
if (this.isProjectJSONFile(resource) && (matches(location, ['dependencies']) || matches(location, ['frameworks', '*', 'dependencies']) || matches(location, ['frameworks', '*', 'frameworkAssemblies']))) {
|
||||
|
||||
return this.getNugetService('SearchAutocompleteService').then(service => {
|
||||
let queryUrl : string;
|
||||
@@ -178,8 +175,8 @@ export class ProjectJSONContribution implements IJSONWorkerContribution {
|
||||
return null;
|
||||
}
|
||||
|
||||
public collectValueSuggestions(resource: string, location: JSONLocation, currentKey: string, result: ISuggestionsCollector): Thenable<any> {
|
||||
if (this.isProjectJSONFile(resource) && (location.matches(['dependencies']) || location.matches(['frameworks', '*', 'dependencies']) || location.matches(['frameworks', '*', 'frameworkAssemblies']))) {
|
||||
public collectValueCompletions(resource: string, location: JSONPath, currentKey: string, result: CompletionsCollector): Thenable<any> {
|
||||
if (this.isProjectJSONFile(resource) && (matches(location, ['dependencies']) || matches(location, ['frameworks', '*', 'dependencies']) || matches(location, ['frameworks', '*', 'frameworkAssemblies']))) {
|
||||
return this.getNugetService('PackageBaseAddress/3.0.0').then(service => {
|
||||
let queryUrl = service + currentKey + '/index.json';
|
||||
return this.makeJSONRequest<any>(queryUrl).then(obj => {
|
||||
@@ -206,9 +203,9 @@ export class ProjectJSONContribution implements IJSONWorkerContribution {
|
||||
return null;
|
||||
}
|
||||
|
||||
public getInfoContribution(resource: string, location: JSONLocation): Thenable<MarkedString[]> {
|
||||
if (this.isProjectJSONFile(resource) && (location.matches(['dependencies', '*']) || location.matches(['frameworks', '*', 'dependencies', '*']) || location.matches(['frameworks', '*', 'frameworkAssemblies', '*']))) {
|
||||
let pack = location.getSegments()[location.getSegments().length - 1];
|
||||
public getInfoContribution(resource: string, location: JSONPath): Thenable<MarkedString[]> {
|
||||
if (this.isProjectJSONFile(resource) && (matches(location, ['dependencies', '*']) || matches(location, ['frameworks', '*', 'dependencies', '*']) || matches(location, ['frameworks', '*', 'frameworkAssemblies', '*']))) {
|
||||
let pack = <string> location[location.length - 1];
|
||||
|
||||
return this.getNugetService('SearchQueryService').then(service => {
|
||||
let queryUrl = service + '?q=' + encodeURIComponent(pack) +'&take=' + 5;
|
||||
@@ -269,4 +266,16 @@ export class ProjectJSONContribution implements IJSONWorkerContribution {
|
||||
};
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function matches(segments: JSONPath, pattern: string[]) {
|
||||
let k = 0;
|
||||
for (let i = 0; k < pattern.length && i < segments.length; i++) {
|
||||
if (pattern[k] === segments[i] || pattern[k] === '*') {
|
||||
k++;
|
||||
} else if (pattern[k] !== '**') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return k === pattern.length;
|
||||
}
|
||||
Reference in New Issue
Block a user