Skip to content

Commit

Permalink
Merge pull request #1837 from codefori/fix/same_doc_multiple_tabs
Browse files Browse the repository at this point in the history
Stop letting the same document be opened twice
  • Loading branch information
worksofliam authored Feb 16, 2024
2 parents 0eb4007 + 46dc325 commit ed2321c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/api/Tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,27 @@ export namespace Tools {
}
}


/**
* We do this to find previously opened files with the same path, but different case OR readonly flags.
* Without this, it's possible for the same document to be opened twice simply due to the readonly flag.
*/
export function findExistingDocumentUri(uri: vscode.Uri) {
const baseUriString = uriStringWithoutFragment(uri);
const possibleDoc = vscode.workspace.textDocuments.find(document => uriStringWithoutFragment(document.uri) === baseUriString);
return possibleDoc?.uri || uri;
}

/**
* We convert member to lowercase as members are case insensitive.
*/
function uriStringWithoutFragment(uri: vscode.Uri) {
// To lowercase because the URI path is case-insensitive
const baseUri = uri.scheme + `:` + uri.path;
const isCaseSensitive = (uri.scheme === `streamfile` && /^\/QOpenSys\//i.test(uri.path));
return (isCaseSensitive ? baseUri : baseUri.toLowerCase());
}

/**
* Fixes an SQL statement to make it compatible with db2 CLI program QZDFMDB2.
* - Changes `@clCommand` statements into Call `QSYS2.QCMDEX('clCommand')` procedure calls
Expand Down
5 changes: 3 additions & 2 deletions src/api/errors/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { parseErrors } from "./parser";
import { FileError } from "../../typings";
import { getEvfeventFiles } from "../local/actions";
import { GlobalConfiguration } from "../Configuration";
import { Tools } from "../Tools";

const ileDiagnostics = vscode.languages.createDiagnosticCollection(`ILE`);

Expand Down Expand Up @@ -168,9 +169,9 @@ export async function handleEvfeventLines(lines: string[], instance: Instance, e
}
} else {
if (file.startsWith(`/`))
ileDiagnostics.set(vscode.Uri.from({ scheme: `streamfile`, path: file }), diagnostics);
ileDiagnostics.set(Tools.findExistingDocumentUri(vscode.Uri.from({ scheme: `streamfile`, path: file })), diagnostics);
else {
const memberUri = vscode.Uri.from({ scheme: `member`, path: `/${asp}${file}${evfeventInfo.extension ? `.` + evfeventInfo.extension : ``}` });
const memberUri = Tools.findExistingDocumentUri(vscode.Uri.from({ scheme: `member`, path: `/${asp}${file}${evfeventInfo.extension ? `.` + evfeventInfo.extension : ``}` }));
ileDiagnostics.set(memberUri, diagnostics);
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/instantiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Instance from "./api/Instance";
import { Search } from "./api/Search";
import { Terminal } from './api/Terminal';
import { refreshDiagnosticsFromServer } from './api/errors/diagnostics';
import { QSysFS, getUriFromPath } from "./filesystems/qsys/QSysFs";
import { QSysFS, getUriFromPath, parseFSOptions } from "./filesystems/qsys/QSysFs";
import { init as clApiInit } from "./languages/clle/clApi";
import * as clRunner from "./languages/clle/clRunner";
import { initGetNewLibl } from "./languages/clle/getnewlibl";
Expand Down Expand Up @@ -120,6 +120,18 @@ export async function loadAllofExtension(context: vscode.ExtensionContext) {
}

const uri = getUriFromPath(path, options);

const existingUri = Tools.findExistingDocumentUri(uri);

if (existingUri) {
const existingOptions = parseFSOptions(existingUri);
if (existingOptions.readonly !== options.readonly) {
vscode.window.showWarningMessage(`The file is already opened in another mode.`);
vscode.window.showTextDocument(existingUri);
return false;
}
}

try {
await vscode.commands.executeCommand(`vscode.openWith`, uri, 'default', { selection: options.position } as vscode.TextDocumentShowOptions);

Expand Down

0 comments on commit ed2321c

Please sign in to comment.