Skip to content

Commit 0bd0bf1

Browse files
author
Dave Bartolomeo
committed
_Correctly_ emulate VS Code's saveBeforeStart
1 parent b182d7a commit 0bd0bf1

17 files changed

Lines changed: 106 additions & 202 deletions

File tree

extensions/ql-vscode/src/local-queries/local-queries.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ export class LocalQueries extends DisposableObject {
358358
range?: Range,
359359
templates?: Record<string, string>,
360360
): Promise<CoreCompletedQuery> {
361+
await saveBeforeStart();
362+
361363
let queryPath: string;
362364
if (queryUri !== undefined) {
363365
// The query URI is provided by the command, most likely because the command was run from an
@@ -388,8 +390,6 @@ export class LocalQueries extends DisposableObject {
388390
const additionalPacks = getOnDiskWorkspaceFolders();
389391
const extensionPacks = await this.getDefaultExtensionPacks(additionalPacks);
390392

391-
await saveBeforeStart();
392-
393393
const coreQueryRun = this.queryRunner.createQueryRun(
394394
databaseItem.databaseUri.fsPath,
395395
{

extensions/ql-vscode/src/run-queries-shared.ts

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ import * as messages from "./pure/messages-shared";
22
import * as legacyMessages from "./pure/legacy-messages";
33
import { DatabaseInfo, QueryMetadata } from "./common/interface-types";
44
import { join, parse, dirname, basename } from "path";
5-
import {
6-
Range,
7-
TextDocument,
8-
TextEditor,
9-
Uri,
10-
window,
11-
workspace,
12-
} from "vscode";
5+
import { Range, TextEditor, Uri, window, workspace } from "vscode";
136
import { isCanary, VSCODE_SAVE_BEFORE_START_SETTING } from "./config";
147
import {
158
pathExists,
@@ -503,61 +496,29 @@ export async function saveBeforeStart(): Promise<void> {
503496
(VSCODE_SAVE_BEFORE_START_SETTING.getValue<string>() as SaveBeforeStartMode) ??
504497
"nonUntitledEditorsInActiveGroup";
505498

499+
// Despite the names of the modes, the VS Code implementation doesn't restrict itself to the
500+
// current tab group. It saves all dirty files in all groups. We'll do the same.
506501
switch (mode) {
507502
case "nonUntitledEditorsInActiveGroup":
508-
await saveAllInGroup(false);
503+
await workspace.saveAll(false);
509504
break;
510505

511506
case "allEditorsInActiveGroup":
512-
await saveAllInGroup(true);
507+
// The VS Code implementation of this mode only saves an untitled file if it is the document
508+
// in the active editor. That's too much work for us, so we'll just live with the inconsistency.
509+
await workspace.saveAll(true);
513510
break;
514511

515512
case "none":
516513
break;
517514

518515
default:
519516
// Unexpected value. Fall back to the default behavior.
520-
await saveAllInGroup(false);
517+
await workspace.saveAll(false);
521518
break;
522519
}
523520
}
524521

525-
// Used in tests
526-
export async function saveAllInGroup(includeUntitled: boolean): Promise<void> {
527-
// There's no good way to get from a `Tab` to a `TextDocument`, so we'll collect all of the dirty
528-
// documents indexed by their URI, and then compare those URIs against the URIs of the tabs.
529-
const dirtyDocumentUris = new Map<string, TextDocument>();
530-
for (const openDocument of workspace.textDocuments) {
531-
if (openDocument.isDirty) {
532-
console.warn(`${openDocument.uri.toString()} is dirty.`);
533-
if (!openDocument.isUntitled || includeUntitled) {
534-
dirtyDocumentUris.set(openDocument.uri.toString(), openDocument);
535-
}
536-
}
537-
}
538-
if (dirtyDocumentUris.size > 0) {
539-
const tabGroup = window.tabGroups.activeTabGroup;
540-
for (const tab of tabGroup.tabs) {
541-
const input = tab.input;
542-
// The `input` property can be of an arbitrary type, depending on the underlying tab type. For
543-
// text editors (and potentially others), it's an object with a `uri` property. That's all we
544-
// need to know to match it up with a dirty document.
545-
if (typeof input === "object") {
546-
const uri = (input as any).uri;
547-
if (uri instanceof Uri) {
548-
const document = dirtyDocumentUris.get(uri.toString());
549-
if (document !== undefined) {
550-
await document.save();
551-
// Remove the URI from the dirty list so we don't wind up saving the same file twice
552-
// if it's open in multiple editors.
553-
dirtyDocumentUris.delete(uri.toString());
554-
}
555-
}
556-
}
557-
}
558-
}
559-
}
560-
561522
/**
562523
* @param filePath This needs to be equivalent to Java's `Path.toRealPath(NO_FOLLOW_LINKS)`
563524
*/

extensions/ql-vscode/test/vscode-tests/cli-integration/data/QuickEvalLib.qll renamed to extensions/ql-vscode/test/data/debugger/QuickEvalLib.qll

File renamed without changes.

extensions/ql-vscode/test/vscode-tests/cli-integration/data/QuickEvalQuery.ql renamed to extensions/ql-vscode/test/data/debugger/QuickEvalQuery.ql

File renamed without changes.

extensions/ql-vscode/test/data/debugger/clean.ql

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
lockVersion: 1.0.0
3+
dependencies:
4+
codeql/javascript-all:
5+
version: 0.6.3
6+
codeql/javascript-queries:
7+
version: 0.6.3
8+
codeql/regex:
9+
version: 0.0.14
10+
codeql/suite-helpers:
11+
version: 0.5.3
12+
codeql/tutorial:
13+
version: 0.0.11
14+
codeql/typos:
15+
version: 0.0.18
16+
codeql/util:
17+
version: 0.0.11
18+
codeql/yaml:
19+
version: 0.0.3
20+
compiled: false

extensions/ql-vscode/test/data/debugger/dirty.ql

Lines changed: 0 additions & 1 deletion
This file was deleted.

extensions/ql-vscode/test/data/debugger/other-clean.ql

Lines changed: 0 additions & 1 deletion
This file was deleted.

extensions/ql-vscode/test/data/debugger/other-dirty.ql

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: integration-test-debugger-javascript
2+
version: 0.0.0
3+
dependencies:
4+
codeql/javascript-queries: "*"

0 commit comments

Comments
 (0)