Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move project.json dependency completion & hover to vscode-omnisharp #1236

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
"fs-extra": "^1.0.0",
"http-proxy-agent": "^1.0.0",
"https-proxy-agent": "^1.0.0",
"jsonc-parser": "^0.3.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update our ThirdPartyNotices.txt for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. That's actually produced by the VS Code team and published by @aeschli here.

"lodash.debounce": "^4.0.8",
"mkdirp": "^0.5.1",
"open": "*",
"request-light": "^0.2.0",
"semver": "*",
"tmp": "0.0.28",
"vscode-debugprotocol": "^1.6.1",
Expand Down
164 changes: 164 additions & 0 deletions src/features/json/jsonContributions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';

import { Location, getLocation, createScanner, SyntaxKind } from 'jsonc-parser';
import { ProjectJSONContribution } from './projectJSONContribution';
import { configure as configureXHR, xhr } from 'request-light';

import {
CompletionItem, CompletionItemProvider, CompletionList, TextDocument, Position, Hover, HoverProvider,
CancellationToken, Range, TextEdit, MarkedString, DocumentSelector, languages, workspace, Disposable
} from 'vscode';

export interface ISuggestionsCollector {
add(suggestion: CompletionItem): void;
error(message: string): void;
log(message: string): void;
setAsIncomplete(): void;
}

export interface IJSONContribution {
getDocumentSelector(): DocumentSelector;
getInfoContribution(fileName: string, location: Location): Thenable<MarkedString[]>;
collectPropertySuggestions(fileName: string, location: Location, currentWord: string, addValue: boolean, isLast: boolean, result: ISuggestionsCollector): Thenable<any>;
collectValueSuggestions(fileName: string, location: Location, result: ISuggestionsCollector): Thenable<any>;
collectDefaultSuggestions(fileName: string, result: ISuggestionsCollector): Thenable<any>;
resolveSuggestion?(item: CompletionItem): Thenable<CompletionItem>;
}

export function addJSONProviders(): Disposable {
let subscriptions: Disposable[] = [];

// configure the XHR library with the latest proxy settings
function configureHttpRequest() {
let httpSettings = workspace.getConfiguration('http');
configureXHR(httpSettings.get<string>('proxy'), httpSettings.get<boolean>('proxyStrictSSL'));
}

configureHttpRequest();
subscriptions.push(workspace.onDidChangeConfiguration(e => configureHttpRequest()));

// register completion and hove providers for JSON setting file(s)
let contributions = [new ProjectJSONContribution(xhr)];
contributions.forEach(contribution => {
let selector = contribution.getDocumentSelector();
subscriptions.push(languages.registerCompletionItemProvider(selector, new JSONCompletionItemProvider(contribution)));
subscriptions.push(languages.registerHoverProvider(selector, new JSONHoverProvider(contribution)));
});

return Disposable.from(...subscriptions);
}

export class JSONHoverProvider implements HoverProvider {

constructor(private jsonContribution: IJSONContribution) {
}

public provideHover(document: TextDocument, position: Position, token: CancellationToken): Thenable<Hover> {
let offset = document.offsetAt(position);
let location = getLocation(document.getText(), offset);
let node = location.previousNode;
if (node && node.offset <= offset && offset <= node.offset + node.length) {
let promise = this.jsonContribution.getInfoContribution(document.fileName, location);
if (promise) {
return promise.then(htmlContent => {
let range = new Range(document.positionAt(node.offset), document.positionAt(node.offset + node.length));
let result: Hover = {
contents: htmlContent,
range: range
};
return result;
});
}
}
return null;
}
}

export class JSONCompletionItemProvider implements CompletionItemProvider {

constructor(private jsonContribution: IJSONContribution) {
}

public resolveCompletionItem(item: CompletionItem, token: CancellationToken): Thenable<CompletionItem> {
if (this.jsonContribution.resolveSuggestion) {
let resolver = this.jsonContribution.resolveSuggestion(item);
if (resolver) {
return resolver;
}
}
return Promise.resolve(item);
}

public provideCompletionItems(document: TextDocument, position: Position, token: CancellationToken): Thenable<CompletionList> {
let currentWord = this.getCurrentWord(document, position);
let overwriteRange = null;
let items: CompletionItem[] = [];
let isIncomplete = false;

let offset = document.offsetAt(position);
let location = getLocation(document.getText(), offset);

let node = location.previousNode;
if (node && node.offset <= offset && offset <= node.offset + node.length && (node.type === 'property' || node.type === 'string' || node.type === 'number' || node.type === 'boolean' || node.type === 'null')) {
overwriteRange = new Range(document.positionAt(node.offset), document.positionAt(node.offset + node.length));
} else {
overwriteRange = new Range(document.positionAt(offset - currentWord.length), position);
}

let proposed: { [key: string]: boolean } = {};
let collector: ISuggestionsCollector = {
add: (suggestion: CompletionItem) => {
if (!proposed[suggestion.label]) {
proposed[suggestion.label] = true;
if (overwriteRange) {
suggestion.textEdit = TextEdit.replace(overwriteRange, <string>suggestion.insertText);
}

items.push(suggestion);
}
},
setAsIncomplete: () => isIncomplete = true,
error: (message: string) => console.error(message),
log: (message: string) => console.log(message)
};

let collectPromise: Thenable<any> = null;

if (location.isAtPropertyKey) {
let addValue = !location.previousNode || !location.previousNode.columnOffset && (offset == (location.previousNode.offset + location.previousNode.length));
let scanner = createScanner(document.getText(), true);
scanner.setPosition(offset);
scanner.scan();
let isLast = scanner.getToken() === SyntaxKind.CloseBraceToken || scanner.getToken() === SyntaxKind.EOF;
collectPromise = this.jsonContribution.collectPropertySuggestions(document.fileName, location, currentWord, addValue, isLast, collector);
} else {
if (location.path.length === 0) {
collectPromise = this.jsonContribution.collectDefaultSuggestions(document.fileName, collector);
} else {
collectPromise = this.jsonContribution.collectValueSuggestions(document.fileName, location, collector);
}
}
if (collectPromise) {
return collectPromise.then(() => {
if (items.length > 0) {
return new CompletionList(items, isIncomplete);
}
return null;
});
}
return null;
}

private getCurrentWord(document: TextDocument, position: Position) {
let i = position.character - 1;
let text = document.lineAt(position.line).text;
while (i >= 0 && ' \t\n\r\v":{[,'.indexOf(text.charAt(i)) === -1) {
i--;
}
return text.substring(i + 1, position.character);
}
}
Loading