-
Notifications
You must be signed in to change notification settings - Fork 76
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
Code completion #209
Code completion #209
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import * as vscode from "vscode"; | ||
import { queryQuickPickTargets } from "../bazel"; | ||
|
||
function InsertCompletionItemIfUnique( | ||
options: vscode.CompletionItem[], | ||
option: vscode.CompletionItem, | ||
) { | ||
if ( | ||
options.find((value: vscode.CompletionItem) => { | ||
return value.label === option.label && value.kind === option.kind; | ||
}) === undefined | ||
) { | ||
options.push(option); | ||
} | ||
} | ||
|
||
function GetCandidateTargetFromDocumentPosition( | ||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
) { | ||
const linePrefix = document | ||
.lineAt(position) | ||
.text.substr(0, position.character); | ||
const index = linePrefix.indexOf('"//'); | ||
if (index === -1) { | ||
return undefined; | ||
} | ||
return linePrefix.substring(index + 1); | ||
} | ||
|
||
function StripLastPackageOrTargetName(target: string) { | ||
const slashIndex = target.lastIndexOf("/"); | ||
const colonIndex = target.lastIndexOf(":"); | ||
const index = Math.max(slashIndex, colonIndex); | ||
if (index !== -1) { | ||
target = target.substring(0, index + 1); | ||
} | ||
return target; | ||
} | ||
|
||
function GetNextPackage(target: string) { | ||
const nextPackage = target.split("/", 2); | ||
if (nextPackage.length > 1) { | ||
return nextPackage[0]; | ||
} else if (nextPackage[0] !== "") { | ||
const withoutTarget = nextPackage[0].split(":", 2); | ||
if (withoutTarget.length > 1) { | ||
return withoutTarget[0]; | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
export class BazelCompletionItemProvider | ||
implements vscode.CompletionItemProvider { | ||
private targets: string[] | undefined; | ||
|
||
public provideCompletionItems( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add comment to briefly explain how the completion works? |
||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
) { | ||
let candidateTarget = GetCandidateTargetFromDocumentPosition( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
document, | ||
position, | ||
); | ||
|
||
if (!candidateTarget.endsWith("/") && !candidateTarget.endsWith(":")) { | ||
candidateTarget = StripLastPackageOrTargetName(candidateTarget); | ||
} | ||
|
||
const completionItems = new Array<vscode.CompletionItem>(); | ||
this.targets.forEach((target) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (!target.startsWith(candidateTarget)) { | ||
return; | ||
} | ||
const sufix = target.replace(candidateTarget, ""); | ||
|
||
let completionKind = vscode.CompletionItemKind.Folder; | ||
let label = GetNextPackage(sufix); | ||
if (label === undefined) { | ||
completionKind = vscode.CompletionItemKind.Field; | ||
label = sufix; | ||
} | ||
InsertCompletionItemIfUnique( | ||
completionItems, | ||
new vscode.CompletionItem(label, completionKind), | ||
); | ||
}); | ||
return completionItems; | ||
} | ||
|
||
public async refresh() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add comment to briefly explain how refresh works and why do we need it? |
||
const queryTargets = await queryQuickPickTargets("kind('.* rule', ...)"); | ||
if (queryTargets.length !== 0) { | ||
this.targets = queryTargets.map((queryTarget) => { | ||
return queryTarget.label; | ||
}); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
export * from "./bazel_completion_provider"; |
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.
Function name should start with a lower case character to keep code style consistent. Please change this and following ones.