-
Notifications
You must be signed in to change notification settings - Fork 101
Adds a CodeAction provider to support fixits #40
Conversation
if (!noSyntaxValidation) { | ||
promises.push(worker.getSyntacticDiagnostics(resource.toString())); | ||
} | ||
if (!noSemanticValidation) { | ||
promises.push(worker.getSemanticDiagnostics(resource.toString())); | ||
} | ||
if (!noSuggestionDiagnostics) { | ||
promises.push(worker.getSuggestionDiagnostics(resource.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.
This also means that JS files should get suggestions too
@@ -188,14 +191,24 @@ export class DiagnostcsAdapter extends Adapter { | |||
const { lineNumber: endLineNumber, column: endColumn } = this._offsetToPosition(resource, diag.start + diag.length); | |||
|
|||
return { | |||
severity: monaco.MarkerSeverity.Error, | |||
severity: this._tsDiagnosticCategoryToMarkerSeverity(diag.category), |
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.
Previously we'd only show only errors, now it can be of many types
src/languageFeatures.ts
Outdated
const start = this._positionToOffset(resource, { lineNumber: range.startLineNumber, column: range.startColumn }); | ||
const end = this._positionToOffset(resource, { lineNumber: range.endLineNumber, column: range.endColumn }); | ||
|
||
// TODO: where to get the current formatting options from? |
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 one I'm still not sure of, it doesn't feel critical - but if anyone has ideas on what the right way to pass in the formatting options, I'd love to know them
kind: codeFix.fixName | ||
}; | ||
|
||
return action; |
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 is my best-guess implementation, leaving most of the work to the edits which come with the original TS code action ideas
@@ -200,6 +206,11 @@ export class TypeScriptWorker implements ts.LanguageServiceHost { | |||
return Promise.resolve(this._languageService.getEmitOutput(fileName)); | |||
} | |||
|
|||
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes:number[], formatOptions: ts.FormatCodeOptions): Promise<ReadonlyArray<ts.CodeFixAction>> { | |||
const preferences = {} | |||
return Promise.resolve(this._languageService.getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences)); |
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 don't think its worth exposing all of the TS code fix options to devs, but I'm open to changing that?
it looks like this:
interface UserPreferences {
readonly disableSuggestions?: boolean;
readonly quotePreference?: "auto" | "double" | "single";
readonly includeCompletionsForModuleExports?: boolean;
readonly includeCompletionsWithInsertText?: boolean;
readonly importModuleSpecifierPreference?: "relative" | "non-relative";
/** Determines whether we import `foo/index.ts` as "foo", "foo/index", or "foo/index.js" */
readonly importModuleSpecifierEnding?: "minimal" | "index" | "js";
readonly allowTextChangesInNewFiles?: boolean;
readonly providePrefixAndSuffixTextForRename?: boolean;
}
test/index.html
Outdated
@@ -165,10 +165,11 @@ <h2>Monaco Editor TypeScript test page</h2> | |||
'var game = new Conway.GameOfLife();', | |||
|
|||
].join('\n'), | |||
language: 'typescript' | |||
language: 'typescript', | |||
lightbulb: { enabled: 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.
Took 30m guessing, then 5m with a debugger on the plane to figure this one out! 🍡 💡
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.
Thank you, this looks great. Two quick problems:
- The hover always shows that no quick fixes are available.
- I get an error when applying a quick fix
@mjbvz Can you please help out with these two things, I don't know what would cause them...
I've looked at this a second time but not entirely got the answer. I think what's happening here is that because I send both the edits and a command to run, then the edits are applied but the command after fails which shows that error. From a look in vscode, it looks like instead it uses a custom command that sends messages which are more or less just sent straight back to the typescript worker to do the editing changes instead. Will take a stab at that |
This... Is tricky. The ts/js language implementations have no access to an instance of an editor. To handle the callback in a similar manner to vscode, I need an instance of the editor to register a command for the code actions to run. There are two routes I can do as-is:
Neither are great options, though I may go for the former. |
Do you need to register a command here at all? From the documentation in vscode.d.ts I get the impression that edits would be sufficient. At least that's the only thing I supply when hooking up ESLint to Monaco. It would be interesting to know what an additional command would be used for. |
Also: The |
I do if I want to let typescript handle making the changes to the code (which is what happens in vscode ) - there's no other route for getting the callback that a user has selected it. Maybe I can just have a version which just applies the code edits which TypeScript passes and call it at that.
<3 |
Thanks @spahnke - I've tried this out on a bunch of our fixits and it works as expected. This version doesn't use the TypeScript services to make the changes, but only uses the code edit deltas sent from the original response from the language service which seems to be enough for every fixits I tried from in the typescript test suite. Should be good to go. |
This looks great! ❤️ |
Woo, thanks @alexandrudima <3 |
Adds the ability to showcase code actions and suggestions in Monaco-typescript - woo! Now it's feasible for the TS team to showcase code edits :D