-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[typespec-vscode] expose linter rule documentation url in codefixes (#…
…5131) Modifications best-practices: 1、Add a URL to the rule file to point to the relevant document.(for testing) compiler: 1、A new codefix file has been added to handle the Linter Rule doc URL. 2、Added OPEN_RULE_DOC command type. 3、If the diagnostic URL is not empty, add a codefix and process the send request in the server file. typespec-vscode: 1、Define the request and open the received URL document. samples: 1、Add lint-related configurations to the yaml configuration file to define a lowercase model type in the tsp file for functional testing. Such as "model foo {}". Related issues:#3043 --------- Co-authored-by: Rodge Fu <[email protected]>
- Loading branch information
1 parent
8bbdf96
commit 6ffff89
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
.chronus/changes/show-linter-rule-url-in-codefix-2024-10-21-9-17-32.md
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: fix | ||
packages: | ||
- typespec-vscode | ||
--- | ||
|
||
Support 'See Document' quick action to view the details of linter rules |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import vscode from "vscode"; | ||
import { OPEN_URL_COMMAND } from "./vscode-command.js"; | ||
|
||
export function createCodeActionProvider() { | ||
return vscode.languages.registerCodeActionsProvider( | ||
"typespec", | ||
new TypeSpecCodeActionProvider(), | ||
{ | ||
providedCodeActionKinds: TypeSpecCodeActionProvider.providedCodeActionKinds, | ||
}, | ||
); | ||
} | ||
|
||
/** | ||
* Provides code actions corresponding to diagnostic problems. | ||
*/ | ||
export class TypeSpecCodeActionProvider implements vscode.CodeActionProvider { | ||
public static readonly providedCodeActionKinds = [vscode.CodeActionKind.QuickFix]; | ||
|
||
provideCodeActions( | ||
_document: vscode.TextDocument, | ||
_range: vscode.Range | vscode.Selection, | ||
context: vscode.CodeActionContext, | ||
_token: vscode.CancellationToken, | ||
): vscode.CodeAction[] { | ||
// for each diagnostic entry that has the matching `code`, create a code action command | ||
// A CodeAction will only be created if it is a TypeSpec diagnostic and code is an object and has a target attribute | ||
// target attribute is the URL to open | ||
|
||
// target is a Uri type, which corresponds to diagnostic.codeDescription.href in compiler | ||
// When target is empty, it does not exist in the code object, so the code action will not be created | ||
const actions: vscode.CodeAction[] = []; | ||
context.diagnostics.forEach((diagnostic) => { | ||
if ( | ||
diagnostic.source === "TypeSpec" && | ||
diagnostic.code && | ||
typeof diagnostic.code === "object" && | ||
"target" in diagnostic.code && | ||
"value" in diagnostic.code | ||
) { | ||
actions.push( | ||
this.createOpenUrlCodeAction( | ||
diagnostic, | ||
diagnostic.code.target.toString(), | ||
diagnostic.code.value.toString(), | ||
), | ||
); | ||
} | ||
}); | ||
return actions; | ||
} | ||
|
||
private createOpenUrlCodeAction( | ||
diagnostic: vscode.Diagnostic, | ||
url: string, | ||
codeActionTitle: string, | ||
): vscode.CodeAction { | ||
// 'vscode.CodeActionKind.Empty' does not generate a Code Action menu, You must use 'vscode.CodeActionKind.QuickFix' | ||
const action = new vscode.CodeAction( | ||
`See documentation for "${codeActionTitle}"`, | ||
vscode.CodeActionKind.QuickFix, | ||
); | ||
action.command = { | ||
command: OPEN_URL_COMMAND, | ||
title: diagnostic.message, | ||
arguments: [url], | ||
}; | ||
action.diagnostics = [diagnostic]; | ||
return action; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import vscode from "vscode"; | ||
import logger from "./log/logger.js"; | ||
|
||
export const OPEN_URL_COMMAND = "typespec.openUrl"; | ||
|
||
export function createCommandOpenUrl() { | ||
return vscode.commands.registerCommand(OPEN_URL_COMMAND, (url: string) => { | ||
// Although vscode has already dealt with the problem of wrong URL, try catch is still added here. | ||
try { | ||
vscode.env.openExternal(vscode.Uri.parse(url)); | ||
} catch (error) { | ||
logger.error(`Failed to open URL: ${url}`, [error as any]); | ||
} | ||
}); | ||
} |