-
Notifications
You must be signed in to change notification settings - Fork 216
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
Fix: Kiota extension works without a workspace folder open #4958
Merged
Merged
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
498c842
remove workspace warnings
ElinorW 28fd20c
update default output path if no workspace is present
ElinorW 1b06ef6
update state after generation
ElinorW fda85e1
fix switch case
ElinorW b4f501f
add workspace path function
ElinorW 4a3fe3d
Merge branch 'elinor/add-kiota-workspace' into elinor/open-output
ElinorW 4c4ede7
return deleted line
ElinorW 62e57c4
fix: fixes workspace loading when vscode does not have a folder open …
baywet 8bfff81
Merge branch 'elinor/add-kiota-workspace' into elinor/open-output
ElinorW 522d2a9
Merge branch 'elinor/open-output' of https://github.com/microsoft/kio…
ElinorW 935794c
Merge branch 'elinor/add-kiota-workspace' into elinor/open-output
ElinorW f5749db
udapte workspace provider
ElinorW 65228b0
uddate cwd
ElinorW 94364e9
Merge branch 'elinor/open-output' of https://github.com/microsoft/kio…
ElinorW cae8b1c
update directory function
ElinorW 9ffecd3
Merge branch 'elinor/add-kiota-workspace' into elinor/open-output
baywet 1b2c00d
Merge branch 'elinor/open-output' of https://github.com/microsoft/kio…
ElinorW 6561c88
update get workspace directory
ElinorW 90199f5
update workspace function
ElinorW 51f6b44
correct return types
ElinorW 207b780
Merge branch 'elinor/add-kiota-workspace' into elinor/open-output
ElinorW a6ab934
add appPackage as default output path
ElinorW 517427b
Merge branch 'elinor/add-kiota-workspace' into elinor/open-output
ElinorW da954ed
update indentation
ElinorW 0118d78
modify outputPath
ElinorW 05cda48
Merge branch 'elinor/open-output' of https://github.com/microsoft/kio…
ElinorW 927286a
add kiota files for selected output path
ElinorW 6046a3a
Merge branch 'elinor/add-kiota-workspace' into elinor/open-output
ElinorW File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,58 @@ | ||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE } from './constants'; | ||
import { KIOTA_WORKSPACE_FILE } from './constants'; | ||
import { getWorkspaceJsonPath } from './util'; | ||
|
||
const workspaceJsonPath = path.join(vscode.workspace.workspaceFolders?.map(folder => folder.uri.fsPath).join('') || '', KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE); | ||
|
||
export class WorkspaceTreeProvider implements vscode.TreeDataProvider<vscode.TreeItem> { | ||
|
||
constructor(private context: vscode.ExtensionContext) { | ||
void this.ensureKiotaDirectory(); | ||
constructor(public isWSPresent: boolean) { | ||
} | ||
async getChildren(element?: vscode.TreeItem): Promise<vscode.TreeItem[]> { | ||
if (!element) { | ||
return [new vscode.TreeItem(KIOTA_WORKSPACE_FILE, vscode.TreeItemCollapsibleState.None)]; | ||
} | ||
if (!this.isWSPresent) { | ||
return []; | ||
} | ||
if (!element) { | ||
return [new vscode.TreeItem(KIOTA_WORKSPACE_FILE, vscode.TreeItemCollapsibleState.None)]; | ||
} | ||
return []; | ||
} | ||
|
||
getTreeItem(element: vscode.TreeItem): vscode.TreeItem { | ||
if (element) { | ||
element.command = { command: 'kiota.workspace.openWorkspaceFile', title: vscode.l10n.t("Open File"), arguments: [vscode.Uri.file(workspaceJsonPath)], }; | ||
element.command = { | ||
command: 'kiota.workspace.openWorkspaceFile', | ||
title: vscode.l10n.t("Open File"), | ||
arguments: [vscode.Uri.file(getWorkspaceJsonPath())] | ||
}; | ||
element.contextValue = 'file'; | ||
} | ||
return element; | ||
} | ||
} | ||
|
||
private async ensureKiotaDirectory(): Promise<unknown> { | ||
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) { | ||
await vscode.window.showErrorMessage( | ||
vscode.l10n.t("No workspace folder found, open a folder first") | ||
); | ||
return; | ||
} | ||
const kiotaDir = path.dirname(workspaceJsonPath); | ||
try { | ||
await fs.promises.access(kiotaDir); | ||
} catch (error) { | ||
await vscode.window.showErrorMessage( | ||
vscode.l10n.t("Kiota directory not found") | ||
); | ||
} | ||
async function openResource(resource: vscode.Uri): Promise<void> { | ||
await vscode.window.showTextDocument(resource); | ||
} | ||
async function isKiotaWorkspaceFilePresent(): Promise<boolean> { | ||
const workspaceFileDir = path.resolve(getWorkspaceJsonPath()); | ||
try { | ||
await fs.promises.access(workspaceFileDir); | ||
} catch (error) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
export class KiotaWorkspace { | ||
constructor(context: vscode.ExtensionContext) { | ||
const treeDataProvider = new WorkspaceTreeProvider(context); | ||
context.subscriptions.push(vscode.window.createTreeView('kiota.workspace', { treeDataProvider })); | ||
vscode.commands.registerCommand('kiota.workspace.openWorkspaceFile', async (resource) => await this.openResource(resource)); | ||
} | ||
private async openResource(resource: vscode.Uri): Promise<void> { | ||
try{ | ||
await vscode.window.showTextDocument(resource); | ||
} catch (error) { | ||
await fs.promises.writeFile(workspaceJsonPath, Buffer.from('{}')); | ||
await vscode.window.showTextDocument(resource); | ||
} | ||
|
||
} | ||
} | ||
export async function loadTreeView(context: vscode.ExtensionContext): Promise<void> { | ||
const treeDataProvider = new WorkspaceTreeProvider(await isKiotaWorkspaceFilePresent()); | ||
context.subscriptions.push(vscode.workspace.onDidChangeWorkspaceFolders(async () => { | ||
treeDataProvider.isWSPresent = await isKiotaWorkspaceFilePresent(); | ||
await vscode.commands.executeCommand('kiota.workspace.refresh'); // Refresh the tree view when workspace folders change | ||
})); | ||
context.subscriptions.push(vscode.window.createTreeView('kiota.workspace', { treeDataProvider })); | ||
context.subscriptions.push(vscode.commands.registerCommand('kiota.workspace.openWorkspaceFile', openResource)); | ||
context.subscriptions.push(vscode.commands.registerCommand('kiota.workspace.refresh', async () => { | ||
treeDataProvider.isWSPresent = await isKiotaWorkspaceFilePresent(); | ||
})); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on this
Do we also want an else clause that displays the reaons for the generation failure by picking the log entry that matches LogLevel.critical?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can display the error in the case of LogLevel.critical