|
| 1 | +// Copyright 2023 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import * as vscode from "vscode"; |
| 16 | +import { queryQuickPickTargets } from "../bazel"; |
| 17 | + |
| 18 | +function isCompletingInsideRepositoryLabel( |
| 19 | + document: vscode.TextDocument, |
| 20 | + position: vscode.Position, |
| 21 | +) { |
| 22 | + const linePrefix = document |
| 23 | + .lineAt(position) |
| 24 | + .text.substring(0, position.character); |
| 25 | + const startOfRepo = linePrefix.lastIndexOf("@"); |
| 26 | + const endOfRepo = linePrefix.lastIndexOf("//"); |
| 27 | + return startOfRepo !== -1 && (endOfRepo === -1 || endOfRepo < startOfRepo); |
| 28 | +} |
| 29 | + |
| 30 | +function getTargetName(label: string) { |
| 31 | + const colonIndex = label.lastIndexOf(":"); |
| 32 | + if (colonIndex === -1) { |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + return label.substring(colonIndex + 1); |
| 36 | +} |
| 37 | + |
| 38 | +export class BazelRepositoryCompletionItemProvider |
| 39 | + implements vscode.CompletionItemProvider { |
| 40 | + private repositories?: Promise<string[]>; |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns completion items matching the given prefix. |
| 44 | + */ |
| 45 | + public async provideCompletionItems( |
| 46 | + document: vscode.TextDocument, |
| 47 | + position: vscode.Position, |
| 48 | + ) { |
| 49 | + if (!isCompletingInsideRepositoryLabel(document, position)) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + |
| 53 | + const repos = await this.getRepos(); |
| 54 | + const completionItems = repos.map( |
| 55 | + (repo) => |
| 56 | + new vscode.CompletionItem(repo, vscode.CompletionItemKind.Folder), |
| 57 | + ); |
| 58 | + return completionItems; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Runs a bazel query command to acquire all the repositories in the |
| 63 | + * workspace. |
| 64 | + */ |
| 65 | + public async refresh(): Promise<void> { |
| 66 | + await this.queryAndCacheRepos(); |
| 67 | + } |
| 68 | + |
| 69 | + private async getRepos(): Promise<string[]> { |
| 70 | + if (this.repositories) { |
| 71 | + return await this.repositories; |
| 72 | + } |
| 73 | + return await this.queryAndCacheRepos(); |
| 74 | + } |
| 75 | + |
| 76 | + private async queryAndCacheRepos(): Promise<string[]> { |
| 77 | + const queryRepos = async () => { |
| 78 | + const targets = await queryQuickPickTargets( |
| 79 | + "kind('.* rule', //external:*)", |
| 80 | + ); |
| 81 | + return targets.map((target) => getTargetName(target.label)); |
| 82 | + }; |
| 83 | + const deferred = queryRepos(); |
| 84 | + this.repositories = deferred; |
| 85 | + return await deferred; |
| 86 | + } |
| 87 | +} |
0 commit comments