-
Notifications
You must be signed in to change notification settings - Fork 219
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
[typespec] expose linter rule documentation url in codefixes #5131
base: main
Are you sure you want to change the base?
[typespec] expose linter rule documentation url in codefixes #5131
Conversation
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 {}".
❌ There is undocummented changes. Run The following packages have changes but are not documented.
Show changes |
You can try these changes here
|
packages/compiler/src/core/compiler-code-fixes/show-linter-rule-doc-url.codefix.ts
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,10 @@ | |||
import { defineCodeFix } from "../diagnostics.js"; | |||
|
|||
export function createShowLinterRuleDocUrlCodeFix(docUrl: string) { |
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.
This shouldn't be a code fix. It doesn't actually fix anything. It would break a --fix-all flag. This is only relevant to ide and so just need to be injected when creating the Vscode actions.
We are already including the doc url in many other place this is just another(including already in the reported diagnostic to the ide - clickable link)
- typespec-vscode | ||
--- | ||
|
||
[typespec] expose linter rule documentation url in codefixes |
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.
Remove the bracket scope. Those changelog are already scoped to the packages above.
And update accordingly with my other comment
…efix file, and restore the other codes to the original state.
) { | ||
mutate(tspDiag.codefixes).push( | ||
defineCodeFix({ | ||
id: "show-linter-rule-doc-url", |
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.
please extract the common string
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.
As comment above this still shouldn't even be a code fix and would simplify the logic later
for (const fix of tspDiag.codefixes ?? []) { | ||
const currentCmd = | ||
fix.id === "show-linter-rule-doc-url" ? Commands.OPEN_RULE_DOC : Commands.APPLY_CODE_FIX; |
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.
it's better to if-else the fix.id with their own logic to make the code more readable and also makes it easier to extend in the future
const docUrl = params.arguments?.[0] as string; | ||
if (process.platform === "win32") { | ||
// in windows | ||
exec(`start ${docUrl}`); |
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.
please fix the security warning. And make sure these command are tested.
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.
This will also only work on windows. Vscode is spice an open command that abstract that anyway
https://github.com/microsoft/vscode-eslint/blob/main/client/src/client.ts#L304-L305
vsDiag.codeDescription?.href && | ||
tspDiag.codefixes?.findIndex((x) => x.id === "show-linter-rule-doc-url") === -1 | ||
) { | ||
mutate(tspDiag.codefixes).push( |
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.
This will still mutate the program . This most likely needs to be done later right when creating the vs code actions
) { | ||
mutate(tspDiag.codefixes).push( | ||
defineCodeFix({ | ||
id: "show-linter-rule-doc-url", |
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.
As comment above this still shouldn't even be a code fix and would simplify the logic later
packages/samples/tspconfig.yaml
Outdated
@@ -1,2 +1,9 @@ | |||
emit: | |||
- "@typespec/openapi3" | |||
|
|||
linter: |
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.
Would revert that for now, best practice is not a published package and this would give the wrong impression looking at samples
…fix, and then define the command to open the URL
@@ -0,0 +1,54 @@ | |||
import vscode from "vscode"; | |||
|
|||
const COMMAND = "CodeActionProvider.Custom.OpenUrl"; |
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.
it's better to use a more specific name: COMMAND -> OPEN_URL_COMMAND, "CodeActionProvider.Custom.OpenUrl" -> "typespec.openUrl"
); | ||
} | ||
|
||
export function createCommandOpenUrl() { |
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.
the command is not necessary coupling with CodeAction. please create a vscode-command file and move this there
/** | ||
* Provides code actions corresponding to diagnostic problems. | ||
*/ | ||
export class ExtensionCodeActionProvider implements vscode.CodeActionProvider { |
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.
TypeSpecCodeActionProvider
): vscode.CodeAction[] { | ||
// for each diagnostic entry that has the matching `code`, create a code action command | ||
return context.diagnostics | ||
.filter((diagnostic) => typeof diagnostic.code === "object" && "target" in diagnostic.code) |
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.
Shouldn't we check whether the diagnostic is for typespec and has url defined here?
.map((diagnostic) => this.createCommandCodeAction(diagnostic)); | ||
} | ||
|
||
private createCommandCodeAction(diagnostic: vscode.Diagnostic): vscode.CodeAction { |
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.
the name is too general. better to be sth like "createOpenUrlCodeAction"
arguments: [ | ||
typeof diagnostic.code === "object" && "target" in diagnostic.code | ||
? diagnostic.code.target.toString() | ||
: "", |
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.
are we storing url in code.target? And we shouldn't add the codeAction if url is "".
], | ||
}; | ||
action.diagnostics = [diagnostic]; | ||
action.isPreferred = true; |
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.
I dont think this is a preferred codeaction
@@ -1,3 +1,5 @@ | |||
export const enum SettingName { | |||
TspServerPath = "typespec.tsp-server.path", | |||
} | |||
|
|||
export const OPEN_URL_COMMAND = "typespec.openUrl"; |
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.
please move it to be with its command
diagnostic.source === "TypeSpec" && | ||
typeof diagnostic.code === "object" && | ||
"target" in diagnostic.code, | ||
) |
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.
please add comment and check target is not empty
private createOpenUrlCodeAction(diagnostic: vscode.Diagnostic): vscode.CodeAction { | ||
// The target will only exist if the compiler's diagnosis contains the url, so no data will be stored. | ||
if (typeof diagnostic.code === "object" && "target" in diagnostic.code) { | ||
const action = new vscode.CodeAction("Open Document", vscode.CodeActionKind.QuickFix); |
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.
please use Empty kind.
@mzhongl524 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
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