Skip to content

Give visibility information to the ide-server. #2482

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

Merged
merged 4 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions extensions/ql-vscode/src/codeql-cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,13 @@ export class CliVersionConstraint {

public static CLI_VERSION_GLOBAL_CACHE = new SemVer("2.12.4");

/**
* CLI version where the langauge server supports visisbility change notifications.
*/
public static CLI_VERSION_WITH_VISIBILITY_NOTIFICATIONS = new SemVer(
"2.14.0",
);

constructor(private readonly cli: CodeQLCliServer) {
/**/
}
Expand Down Expand Up @@ -1917,4 +1924,10 @@ export class CliVersionConstraint {
async usesGlobalCompilationCache() {
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_GLOBAL_CACHE);
}

async supportsVisibilityNotifications() {
return this.isVersionAtLeast(
CliVersionConstraint.CLI_VERSION_WITH_VISIBILITY_NOTIFICATIONS,
);
}
}
38 changes: 16 additions & 22 deletions extensions/ql-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import {
import {
AstViewer,
install,
spawnIdeServer,
getQueryEditorCommands,
TemplatePrintAstProvider,
TemplatePrintCfgProvider,
TemplateQueryDefinitionProvider,
TemplateQueryReferenceProvider,
createIDEServer,
} from "./language-support";
import { DatabaseManager } from "./databases/local-databases";
import { DatabaseUI } from "./databases/local-databases-ui";
Expand Down Expand Up @@ -903,24 +903,7 @@ async function activateWithInstalledDistribution(
ctx.subscriptions.push(tmpDirDisposal);

void extLogger.log("Initializing CodeQL language server.");
const client = new LanguageClient(
"codeQL.lsp",
"CodeQL Language Server",
() => spawnIdeServer(qlConfigurationListener),
{
documentSelector: [
{ language: "ql", scheme: "file" },
{ language: "yaml", scheme: "file", pattern: "**/qlpack.yml" },
{ language: "yaml", scheme: "file", pattern: "**/codeql-pack.yml" },
],
synchronize: {
configurationSection: "codeQL",
},
// Ensure that language server exceptions are logged to the same channel as its output.
outputChannel: ideServerLogger.outputChannel,
},
true,
);
const ideServer = createIDEServer(qlConfigurationListener);

const localQueries = new LocalQueries(
app,
Expand Down Expand Up @@ -1002,7 +985,7 @@ async function activateWithInstalledDistribution(
void extLogger.log("Registering top-level command palette commands.");

const allCommands: AllExtensionCommands = {
...getCommands(app, cliServer, qs, client),
...getCommands(app, cliServer, qs, ideServer),
...getQueryEditorCommands({
commandManager: app.commands,
queryRunner: qs,
Expand Down Expand Up @@ -1048,12 +1031,23 @@ async function activateWithInstalledDistribution(
}

void extLogger.log("Starting language server.");
await client.start();
await ideServer.start();
ctx.subscriptions.push({
dispose: () => {
void client.stop();
void ideServer.stop();
},
});

// Handle visibility changes in the ideserver
if (await cliServer.cliConstraints.supportsVisibilityNotifications()) {
Window.onDidChangeVisibleTextEditors((editors) => {
ideServer.notifyVisibilityChange(editors);
});
// Send an inital notification to the language server
// to set the initial state of the visible editors.
ideServer.notifyVisibilityChange(Window.visibleTextEditors);
}

// Jump-to-definition and find-references
void extLogger.log("Registering jump-to-definition handlers.");

Expand Down
66 changes: 61 additions & 5 deletions extensions/ql-vscode/src/language-support/ide-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ProgressLocation, window } from "vscode";
import { StreamInfo } from "vscode-languageclient/node";
import { ProgressLocation, TextEditor, window } from "vscode";
import {
LanguageClient,
NotificationType,
StreamInfo,
} from "vscode-languageclient/node";
import { shouldDebugIdeServer, spawnServer } from "../codeql-cli/cli";
import { QueryServerConfig } from "../config";
import { ideServerLogger } from "../common";
Expand All @@ -8,10 +12,52 @@ import { ideServerLogger } from "../common";
* Managing the language server for CodeQL.
*/

/** Starts a new CodeQL language server process, sending progress messages to the status bar. */
export async function spawnIdeServer(
/**
* Create a new CodeQL language server.
*/
export function createIDEServer(
config: QueryServerConfig,
): Promise<StreamInfo> {
): CodeQLLanguageClient {
return new CodeQLLanguageClient(config);
}

/**
* CodeQL language server.
*/
export class CodeQLLanguageClient extends LanguageClient {
constructor(config: QueryServerConfig) {
super(
"codeQL.lsp",
"CodeQL Language Server",
() => spawnIdeServer(config),
{
documentSelector: [
{ language: "ql", scheme: "file" },
{ language: "yaml", scheme: "file", pattern: "**/qlpack.yml" },
{ language: "yaml", scheme: "file", pattern: "**/codeql-pack.yml" },
],
synchronize: {
configurationSection: "codeQL",
},
// Ensure that language server exceptions are logged to the same channel as its output.
outputChannel: ideServerLogger.outputChannel,
},
true,
);
}

notifyVisibilityChange(editors: readonly TextEditor[]) {
const files = editors
.filter((e) => e.document.uri.scheme === "file")
.map((e) => e.document.uri.toString());
void this.sendNotification(didChangeVisibileFiles, {
visibleFiles: files,
});
}
}

/** Starts a new CodeQL language server process, sending progress messages to the status bar. */
async function spawnIdeServer(config: QueryServerConfig): Promise<StreamInfo> {
return window.withProgress(
{ title: "CodeQL language server", location: ProgressLocation.Window },
async (progressReporter, _) => {
Expand All @@ -37,3 +83,13 @@ export async function spawnIdeServer(
},
);
}

/**
* Custom notification type for when the set of visible files changes.
*/
interface DidChangeVisibileFilesParams {
visibleFiles: string[];
}

const didChangeVisibileFiles: NotificationType<DidChangeVisibileFilesParams> =
new NotificationType("textDocument/codeQLDidChangeVisibleFiles");