Skip to content
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

Multiple quick fixes and refactoring #3973

Merged
merged 4 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions libs/remix-ui/editor/src/lib/providers/codeActionProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Monaco } from "@monaco-editor/react"
import monaco from "../../types/monaco"
import { EditorUIProps } from "../remix-ui-editor"
import { default as fixes } from "./quickfixes"
import { default as fixesList } from "./quickfixes"

export class RemixCodeActionProvider implements monaco.languages.CodeActionProvider {
props: EditorUIProps
Expand All @@ -12,40 +12,39 @@ export class RemixCodeActionProvider implements monaco.languages.CodeActionProvi
}

async provideCodeActions (
model: monaco.editor.ITextModel /**ITextModel*/,
range: monaco.Range /**Range*/,
context: monaco.languages.CodeActionContext /**CodeActionContext*/,
token: monaco.CancellationToken /**CancellationToken*/
model: monaco.editor.ITextModel,
range: monaco.Range,
context: monaco.languages.CodeActionContext,
token: monaco.CancellationToken
): Promise<monaco.languages.CodeActionList> {
const actions: monaco.languages.CodeAction[] = []
for (const error of context.markers) {
let fix: Record<string, any>
let msg: string
const errStrings: string[] = Object.keys(fixes)
let fixes: Record<string, any>[], msg: string
let isOldAST: boolean = false
const errStrings: string[] = Object.keys(fixesList)
const errStr:string = errStrings.find(es => error.message.includes(es))
if (errStr) {
fix = fixes[errStr]
fixes = fixesList[errStr]
const cursorPosition: number = this.props.editorAPI.getHoverPosition({lineNumber: error.startLineNumber, column: error.startColumn})
const nodeAtPosition = await this.props.plugin.call('codeParser', 'definitionAtPosition', cursorPosition)
// Check if a function is hovered
if (nodeAtPosition && nodeAtPosition.nodeType === "FunctionDefinition") {
// Identify type of AST node
if (nodeAtPosition.parameters && !Array.isArray(nodeAtPosition.parameters) && Array.isArray(nodeAtPosition.parameters.parameters)) {
const paramNodes = nodeAtPosition.parameters.parameters
// If method has parameters
if (paramNodes.length) msg = await this.fixForMethodWithParams(model, paramNodes, fix, error, true)
else msg = await this.fixForMethodWithoutParams(model, nodeAtPosition, fix, error, true)
} else {
const paramNodes = nodeAtPosition.parameters
// If method has parameters
if (paramNodes.length) msg = await this.fixForMethodWithParams(model, paramNodes, fix, error, false)
else msg = await this.fixForMethodWithoutParams(model, nodeAtPosition, fix, error, false)
if (nodeAtPosition.parameters && !Array.isArray(nodeAtPosition.parameters) && Array.isArray(nodeAtPosition.parameters.parameters))
isOldAST = true
const paramNodes = isOldAST ? nodeAtPosition.parameters.parameters : nodeAtPosition.parameters
for (const fix of fixes) {
msg = paramNodes.length
? await this.fixForMethodWithParams(model, paramNodes, fix, error, isOldAST)
: await this.fixForMethodWithoutParams(model, nodeAtPosition, fix, error, isOldAST)
this.addQuickFix(actions, error, model.uri, {title: fix.title, range: fix.range, text: msg})
}
} else if (fix && nodeAtPosition && fix.nodeType !== nodeAtPosition.nodeType) return
if (Array.isArray(fix))
for (const element of fix)
this.addQuickFix(actions, error, model.uri, {title: element.title, range: element.range || error, text: msg || element.message})
else this.addQuickFix(actions, error, model.uri, {title: fix.title, range: fix.range || error, text: msg || fix.message})
} else {
for (const fix of fixes) {
if (fix && nodeAtPosition && fix.nodeType !== nodeAtPosition.nodeType) continue
else this.addQuickFix(actions, error, model.uri, {title: fix.title, range: fix.range || error, text: fix.message})
}
}
}
}

Expand Down
33 changes: 24 additions & 9 deletions libs/remix-ui/editor/src/lib/providers/quickfixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
"title": "Add non-open-source license",
"message": "// SPDX-License-Identifier: UNLICENSED"
}],
"Warning: Source file does not specify required compiler version! Consider adding" : {
"Warning: Source file does not specify required compiler version! Consider adding" : [{
"id": 2,
"title": "Add Solidity pragma",
"message": "pragma solidity ^0.*.*;",
Expand All @@ -21,23 +21,38 @@ export default {
startColumn: 1,
endColumn: 1
}
},
"SyntaxError: No visibility specified. Did you intend to add \"public\"": {
"id": 3,
}],
"SyntaxError: No visibility specified. Did you intend to add \"public\"": [{
"id": 3.1,
"title": "Add visibility 'public'",
"message": "public ",
"nodeType": "FunctionDefinition"
},
"Warning: Function state mutability can be restricted to view": {
},{
"id": 3.2,
"title": "Add visibility 'private'",
"message": "private ",
"nodeType": "FunctionDefinition"
},{
"id": 3.3,
"title": "Add visibility 'internal'",
"message": "internal ",
"nodeType": "FunctionDefinition"
},{
"id": 3.4,
"title": "Add visibility 'external'",
"message": "external ",
"nodeType": "FunctionDefinition"
}],
"Warning: Function state mutability can be restricted to view": [{
"id": 4,
"title": "Add mutability 'view'",
"message": "view ",
"nodeType": "FunctionDefinition"
},
"Warning: Function state mutability can be restricted to pure": {
}],
"Warning: Function state mutability can be restricted to pure": [{
"id": 5,
"title": "Add mutability 'pure'",
"message": "pure ",
"nodeType": "FunctionDefinition"
}
}]
}
Loading