Skip to content

Commit 7d12320

Browse files
committed
Refactored all file URI usages to use a common utility method for consistent handling and to avoid ambiguity between Uri.file and Uri.parse.
1 parent 5e22af4 commit 7d12320

File tree

15 files changed

+461
-803
lines changed

15 files changed

+461
-803
lines changed

vscode/package-lock.json

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vscode/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,8 @@
933933
"sinon": "^20.0.0",
934934
"ts-mockito": "^2.6.1",
935935
"ts-node": "^10.9.2",
936-
"typescript": "^5.8.3"
936+
"typescript": "^5.8.3",
937+
"vscode-uri": "^3.1.0"
937938
},
938939
"dependencies": {
939940
"@vscode/debugadapter": "^1.68.0",

vscode/src/commands/create.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
import { workspace, commands, Uri, window } from "vscode";
16+
import { workspace, commands, window } from "vscode";
1717
import { LanguageClient } from "vscode-languageclient/node";
1818
import { nbCommands, builtInCommands, extCommands } from "./commands";
1919
import { l10n } from "../localiser";
2020
import * as os from 'os';
2121
import * as fs from 'fs';
2222
import { ICommand } from "./types";
23-
import { getContextUri, isNbCommandRegistered } from "./utils";
24-
import { isString } from "../utils";
23+
import { getContextUriFromFile, isNbCommandRegistered } from "./utils";
24+
import { FileUtils, isString } from "../utils";
2525
import { globalState } from "../globalState";
2626

2727
const newFromTemplate = async (ctx: any, template: any) => {
@@ -40,7 +40,7 @@ const newFromTemplate = async (ctx: any, template: any) => {
4040
if (!fs.existsSync(folderPath)) {
4141
await fs.promises.mkdir(folderPath);
4242
}
43-
const folderPathUri = Uri.file(folderPath);
43+
const folderPathUri = FileUtils.toUri(folderPath);
4444
await commands.executeCommand(nbCommands.newFromTemplate, folderPathUri.toString());
4545
await commands.executeCommand(builtInCommands.openFolder, folderPathUri);
4646

@@ -52,16 +52,16 @@ const newFromTemplate = async (ctx: any, template: any) => {
5252
if (isString(template)) {
5353
params.push(template);
5454
}
55-
params.push(getContextUri(ctx)?.toString(), window.activeTextEditor?.document?.uri?.toString());
55+
params.push(getContextUriFromFile(ctx)?.toString(), window.activeTextEditor?.document?.uri?.toString());
5656
const res = await commands.executeCommand(nbCommands.newFromTemplate, ...params);
5757

5858
if (isString(res)) {
59-
let newFile = Uri.parse(res as string);
59+
let newFile = FileUtils.toUri(res as string, true);
6060
await window.showTextDocument(newFile, { preview: false });
6161
} else if (Array.isArray(res)) {
6262
for (let r of res) {
6363
if (isString(r)) {
64-
let newFile = Uri.parse(r as string);
64+
let newFile = FileUtils.toUri(r as string, true);
6565
await window.showTextDocument(newFile, { preview: false });
6666
}
6767
}
@@ -74,9 +74,9 @@ const newFromTemplate = async (ctx: any, template: any) => {
7474
const newProject = async (ctx: any) => {
7575
const client: LanguageClient = await globalState.getClientPromise().client;
7676
if (await isNbCommandRegistered(nbCommands.newProject)) {
77-
const res = await commands.executeCommand(nbCommands.newProject, getContextUri(ctx)?.toString());
77+
const res = await commands.executeCommand(nbCommands.newProject, getContextUriFromFile(ctx)?.toString());
7878
if (isString(res)) {
79-
let newProject = Uri.parse(res as string);
79+
let newProject = FileUtils.toUri(res as string, true);
8080

8181
const OPEN_IN_NEW_WINDOW = l10n.value("jdk.extension.label.openInNewWindow");
8282
const ADD_TO_CURRENT_WORKSPACE = l10n.value("jdk.extension.label.addToWorkSpace");

vscode/src/commands/debug.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616

1717
import * as vscode from 'vscode';
18-
import { builtInCommands, extCommands } from "./commands";
18+
import { extCommands } from "./commands";
1919
import { ICommand } from "./types";
2020
import { extConstants } from '../constants';
21-
import { getContextUri } from './utils';
21+
import { getContextUriFromFile } from './utils';
2222

2323
const runTest = async (uri: any, methodName? : string, launchConfiguration?: string) => {
2424
await runDebug(true, true, uri, methodName, launchConfiguration);
@@ -33,23 +33,23 @@ const debugSingle = async (uri: any, methodName? : string, launchConfiguration?:
3333
await runDebug(false, false, uri, methodName, launchConfiguration);
3434
}
3535
const projectRun = async (node: any, launchConfiguration? : string) => {
36-
return runDebug(true, false, getContextUri(node)?.toString() || '', undefined, launchConfiguration, true);
36+
return runDebug(true, false, getContextUriFromFile(node)?.toString() || '', undefined, launchConfiguration, true);
3737
}
3838
const projectDebug = async (node: any, launchConfiguration? : string) => {
39-
return runDebug(false, false, getContextUri(node)?.toString() || '', undefined, launchConfiguration, true);
39+
return runDebug(false, false, getContextUriFromFile(node)?.toString() || '', undefined, launchConfiguration, true);
4040
}
4141
const projectTest = async (node: any, launchConfiguration? : string) => {
42-
return runDebug(true, true, getContextUri(node)?.toString() || '', undefined, launchConfiguration, true);
42+
return runDebug(true, true, getContextUriFromFile(node)?.toString() || '', undefined, launchConfiguration, true);
4343
}
4444
const projectTestDebug = async (node: any, launchConfiguration? : string) => {
45-
return runDebug(false, true, getContextUri(node)?.toString() || '', undefined, launchConfiguration, true);
45+
return runDebug(false, true, getContextUriFromFile(node)?.toString() || '', undefined, launchConfiguration, true);
4646
}
4747
const packageTest = async (uri: any, launchConfiguration? : string) => {
4848
await runDebug(true, true, uri, undefined, launchConfiguration);
4949
}
5050

5151
const runDebug = async (noDebug: boolean, testRun: boolean, uri: any, methodName?: string, launchConfiguration?: string, project : boolean = false, ) => {
52-
const docUri = getContextUri(uri);
52+
const docUri = getContextUriFromFile(uri);
5353
if (docUri) {
5454
let debugConfig : vscode.DebugConfiguration = {
5555
type: extConstants.COMMAND_PREFIX,

vscode/src/commands/navigation.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
import { commands, Position, window, Selection, Range, Uri } from "vscode";
16+
import { commands, Position, window, Selection, Range } from "vscode";
1717
import { builtInCommands, extCommands, nbCommands } from "./commands";
1818
import { l10n } from "../localiser";
1919
import * as path from 'path';
2020
import { ICommand } from "./types";
2121
import { LanguageClient } from "vscode-languageclient/node";
2222
import { LOGGER } from '../logger';
23-
import { getContextUri, isNbCommandRegistered, wrapCommandWithProgress } from "./utils";
23+
import { getContextUriFromFile, isNbCommandRegistered, wrapCommandWithProgress } from "./utils";
2424
import { globalState } from "../globalState";
25+
import { FileUtils } from "../utils";
2526

2627
const goToTest = async (ctx: any) => {
2728
let client: LanguageClient = await globalState.getClientPromise().client;
2829
if (await isNbCommandRegistered(nbCommands.goToTest)) {
2930
try {
30-
const res: any = await commands.executeCommand(nbCommands.goToTest, getContextUri(ctx)?.toString());
31+
const res: any = await commands.executeCommand(nbCommands.goToTest, getContextUriFromFile(ctx)?.toString());
3132
if ("errorMessage" in res) {
3233
throw new Error(res.errorMessage);
3334
}
@@ -39,7 +40,8 @@ const goToTest = async (ctx: any) => {
3940
if (res?.locations?.length) {
4041
if (res.locations.length === 1) {
4142
const { file, offset } = res.locations[0];
42-
const filePath = Uri.parse(file);
43+
// If in the future the GoToTest returns URI locations then pass true as an extra parameter to FileUtils.toUri
44+
const filePath = FileUtils.toUri(file);
4345
const editor = await window.showTextDocument(filePath, { preview: false });
4446
if (offset != -1) {
4547
const pos: Position = editor.document.positionAt(offset);
@@ -61,7 +63,8 @@ const goToTest = async (ctx: any) => {
6163
});
6264
if (selected) {
6365
for await (const filePath of selected) {
64-
let file = Uri.parse(filePath);
66+
// If in the future the GoToTest returns URI locations then pass true as an extra parameter to FileUtils.toUri
67+
let file = FileUtils.toUri(filePath);
6568
await window.showTextDocument(file, { preview: false });
6669
}
6770
} else {
@@ -87,7 +90,7 @@ const openStackHandler = async (uri: any, methodName: any, fileName: any, line:
8790
const location: string | undefined = uri ? await commands.executeCommand(nbCommands.resolveStackLocation, uri, methodName, fileName) : undefined;
8891
if (location) {
8992
const lNum = line - 1;
90-
window.showTextDocument(Uri.parse(location), { selection: new Range(new Position(lNum, 0), new Position(lNum, 0)) });
93+
window.showTextDocument(FileUtils.toUri(location, true), { selection: new Range(new Position(lNum, 0), new Position(lNum, 0)) });
9194
} else {
9295
if (methodName) {
9396
const fqn: string = methodName.substring(0, methodName.lastIndexOf('.'));

vscode/src/commands/notebook.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import * as path from 'path';
33
import * as fs from 'fs';
44
import { LOGGER } from '../logger';
55
import { commands, ConfigurationTarget, Uri, window, workspace } from 'vscode';
6-
import { isError } from '../utils';
6+
import { FileUtils, isError } from '../utils';
77
import { extCommands, nbCommands } from './commands';
88
import { ICommand } from './types';
99
import { LanguageClient } from 'vscode-languageclient/node';
1010
import { globalState } from '../globalState';
11-
import { getContextUri, isNbCommandRegistered } from './utils';
11+
import { getContextUriFromFile, isNbCommandRegistered } from './utils';
1212
import { l10n } from '../localiser';
1313
import { extConstants } from '../constants';
1414
import { Notebook } from '../notebooks/notebook';
@@ -26,7 +26,7 @@ const createNewNotebook = async (ctx?: any) => {
2626
const activeFilePath = window.activeTextEditor?.document.uri;
2727

2828
if (activeFilePath) {
29-
const parentDir = Uri.parse(path.dirname(activeFilePath.fsPath));
29+
const parentDir = FileUtils.toUri(path.dirname(activeFilePath.fsPath));
3030
if (workspace.getWorkspaceFolder(parentDir)) {
3131
defaultUri = parentDir;
3232
}
@@ -46,7 +46,7 @@ const createNewNotebook = async (ctx?: any) => {
4646
}
4747
}
4848
if (defaultUri == null) {
49-
defaultUri = Uri.parse(os.homedir());
49+
defaultUri = FileUtils.toUri(os.homedir());
5050
}
5151
}
5252

@@ -63,7 +63,7 @@ const createNewNotebook = async (ctx?: any) => {
6363
notebookDir = nbFolderPath[0];
6464
}
6565
} else {
66-
notebookDir = getContextUri(ctx) || null;
66+
notebookDir = getContextUriFromFile(ctx) || null;
6767
}
6868
if (notebookDir == null) {
6969
window.showErrorMessage(l10n.value("jdk.notebook.create.error_msg.path.not.selected"));
@@ -109,7 +109,7 @@ const createNewNotebook = async (ctx?: any) => {
109109

110110
LOGGER.log(`Created notebook at: ${finalNotebookPath}`);
111111

112-
const notebookUri = Uri.file(finalNotebookPath);
112+
const notebookUri = FileUtils.toUri(finalNotebookPath);
113113
const notebookDocument = await workspace.openNotebookDocument(notebookUri);
114114
await window.showNotebookDocument(notebookDocument);
115115
} catch (error) {

vscode/src/commands/refactor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
import { commands, window, Uri, Range, Location, workspace, Position } from "vscode";
16+
import { commands, window, Range, Location, workspace, Position } from "vscode";
1717
import { ICommand } from "./types";
1818
import { extConstants } from "../constants";
1919
import { builtInCommands, extCommands, nbCommands } from "./commands";
2020
import { l10n } from "../localiser";
2121
import { WorkspaceEdit } from 'vscode-languageserver-protocol';
2222
import { SymbolInformation } from 'vscode-languageclient';
2323
import { globalState } from "../globalState";
24+
import { FileUtils } from "../utils";
2425

2526
const goToSuperImplementationHandler = async () => {
2627
if (window.activeTextEditor?.document.languageId !== extConstants.LANGUAGE_ID) {
@@ -30,7 +31,7 @@ const goToSuperImplementationHandler = async () => {
3031
const position = window.activeTextEditor.selection.active;
3132
const locations: any[] = await commands.executeCommand(nbCommands.superImpl, uri.toString(), position) || [];
3233
return commands.executeCommand(builtInCommands.goToEditorLocations, window.activeTextEditor.document.uri, position,
33-
locations.map(location => new Location(Uri.parse(location.uri), new Range(location.range.start.line, location.range.start.character, location.range.end.line, location.range.end.character))),
34+
locations.map(location => new Location(FileUtils.toUri(location.uri, true), new Range(location.range.start.line, location.range.start.character, location.range.end.line, location.range.end.character))),
3435
'peek', l10n.value('jdk.extension.error_msg.noSuperImpl'));
3536
}
3637

vscode/src/commands/utils.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@ import { LanguageClient } from "vscode-languageclient/node";
2020
import { l10n } from "../localiser";
2121
import { LOGGER } from "../logger";
2222
import { globalState } from "../globalState";
23+
import { FileUtils, isString } from "../utils";
2324

24-
export const getContextUri = (ctx: any): Uri | undefined => {
25+
export const getContextUriFromFile = (ctx: any): Uri | undefined => {
2526
if (ctx?.fsPath) {
2627
return ctx as Uri;
2728
}
2829
if (ctx?.resourceUri) {
2930
return ctx.resourceUri as Uri;
3031
}
31-
if (typeof ctx == 'string') {
32-
try {
33-
return Uri.parse(ctx, true);
34-
} catch (err) {
35-
return Uri.file(ctx);
36-
}
32+
if (isString(ctx)) {
33+
return FileUtils.toUri(ctx, true);
3734
}
3835

3936
return window.activeTextEditor?.document?.uri;

vscode/src/localiser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as l10nLib from '@vscode/l10n'
2020

2121
import * as vscode from 'vscode';
2222
import { extConstants } from './constants';
23+
import { FileUtils } from './utils';
2324

2425
const DEFAULT_LANGAUGE = "en";
2526
const DEFAULT_BUNDLE_FILE = `l10n/bundle.l10n.${DEFAULT_LANGAUGE}.json`;
@@ -36,7 +37,7 @@ class l10Wrapper implements l10n {
3637
private defaultTranslation: TranslatorFn;
3738

3839
constructor(extensionId: string, defaultBundlePath: string) {
39-
let defaultBundleAbsoluteFsPath = vscode.Uri.file(`${vscode.extensions.getExtension(extensionId)?.extensionPath}/${defaultBundlePath}`).fsPath
40+
let defaultBundleAbsoluteFsPath = FileUtils.toUri(`${vscode.extensions.getExtension(extensionId)?.extensionPath}/${defaultBundlePath}`).fsPath;
4041
l10nLib.config({
4142
fsPath: defaultBundleAbsoluteFsPath
4243
});

vscode/src/telemetry/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import * as crypto from 'crypto';
1717
import { Uri, workspace } from 'vscode';
1818
import * as os from 'os';
19-
import { isObject, isString } from '../utils';
19+
import { FileUtils, isObject, isString } from '../utils';
2020

2121
export const getCurrentUTCDateInSeconds = () => {
2222
const date = Date.now();
@@ -70,7 +70,7 @@ const getUri = (pathOrUri: Uri | string): Uri => {
7070
if (pathOrUri instanceof Uri) {
7171
return pathOrUri;
7272
}
73-
return Uri.file(pathOrUri);
73+
return FileUtils.toUri(pathOrUri);
7474
}
7575

7676
export const getValuesToBeTransformed = (): string[] => {

0 commit comments

Comments
 (0)