-
Notifications
You must be signed in to change notification settings - Fork 676
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
DustinCampbell
merged 6 commits into
dotnet:master
from
DustinCampbell:project-json-dependency-supports
Feb 16, 2017
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6fa4631
Move project.json dependency completion & hover to vscode-omnisharp
aeschli 02b8b4a
Don't need basename
aeschli e4184ea
Don't activate on onLanguage:json, workspaceContains:project.json is…
aeschli e1e64c0
Merge branch 'aeschli/project-json-dependency-supports' of https://gi…
DustinCampbell 0e2bbb4
Get to successful compile and clean up tslint warnings
DustinCampbell a720c09
Update jsonc-parser and request-light to latest versions
DustinCampbell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.