Skip to content

Commit

Permalink
Return the source path one there is only one
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo committed Aug 12, 2021
1 parent 9d9efa9 commit 0d733d3
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/explorerCommands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,36 @@ async function newUntiledJavaFile(): Promise<void> {
}

async function inferPackageFsPath(): Promise<string> {
let sourcePaths: string[] | undefined;
try {
const result = await commands.executeCommand<ListCommandResult>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.LIST_SOURCEPATHS);
if (result && result.data && result.data.length) {
sourcePaths = result.data.map(entry => entry.path);
}
} catch (e) {
// do nothing
}

if (!window.activeTextEditor) {
if (sourcePaths?.length === 1) {
return sourcePaths[0];
}
return "";
}

const fileUri: Uri = window.activeTextEditor.document.uri;
const workspaceFolder: WorkspaceFolder | undefined = workspace.getWorkspaceFolder(fileUri);
if (!workspaceFolder) {
return "";
}

const filePath: string = window.activeTextEditor.document.uri.fsPath;
try {
const result = await commands.executeCommand<ListCommandResult>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.LIST_SOURCEPATHS);
if (result && result.data && result.data.length) {
for (const sourcePath of result.data) {
if (!path.relative(sourcePath.path, filePath).startsWith("..")) {
return path.dirname(window.activeTextEditor.document.uri.fsPath);
}
if (sourcePaths) {
for (const sourcePath of sourcePaths) {
if (!path.relative(sourcePath, filePath).startsWith("..")) {
return path.dirname(window.activeTextEditor.document.uri.fsPath);
}
}
} catch (e) {
// do nothing
}

return "";
Expand Down

0 comments on commit 0d733d3

Please sign in to comment.