|
|
|
@ -3,9 +3,7 @@ import monaco from '../../types/monaco' |
|
|
|
|
import {EditorUIProps} from '../remix-ui-editor' |
|
|
|
|
import {default as fixesList} from './quickfixes' |
|
|
|
|
|
|
|
|
|
export class RemixCodeActionProvider |
|
|
|
|
implements monaco.languages.CodeActionProvider |
|
|
|
|
{ |
|
|
|
|
export class RemixCodeActionProvider implements monaco.languages.CodeActionProvider { |
|
|
|
|
props: EditorUIProps |
|
|
|
|
monaco: Monaco |
|
|
|
|
constructor(props: any, monaco: any) { |
|
|
|
@ -31,42 +29,16 @@ implements monaco.languages.CodeActionProvider |
|
|
|
|
lineNumber: error.startLineNumber, |
|
|
|
|
column: error.startColumn |
|
|
|
|
}) |
|
|
|
|
const nodeAtPosition = await this.props.plugin.call( |
|
|
|
|
'codeParser', |
|
|
|
|
'definitionAtPosition', |
|
|
|
|
cursorPosition |
|
|
|
|
) |
|
|
|
|
const nodeAtPosition = await this.props.plugin.call('codeParser', 'definitionAtPosition', cursorPosition) |
|
|
|
|
// Check if a function is hovered
|
|
|
|
|
if ( |
|
|
|
|
nodeAtPosition && |
|
|
|
|
nodeAtPosition.nodeType === 'FunctionDefinition' |
|
|
|
|
) { |
|
|
|
|
if (nodeAtPosition && nodeAtPosition.nodeType === 'FunctionDefinition') { |
|
|
|
|
// Identify type of AST node
|
|
|
|
|
if ( |
|
|
|
|
nodeAtPosition.parameters && |
|
|
|
|
!Array.isArray(nodeAtPosition.parameters) && |
|
|
|
|
Array.isArray(nodeAtPosition.parameters.parameters) |
|
|
|
|
) |
|
|
|
|
isOldAST = true |
|
|
|
|
const paramNodes = isOldAST |
|
|
|
|
? nodeAtPosition.parameters.parameters |
|
|
|
|
: nodeAtPosition.parameters |
|
|
|
|
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 |
|
|
|
|
) |
|
|
|
|
? await this.fixForMethodWithParams(model, paramNodes, fix, error, isOldAST) |
|
|
|
|
: await this.fixForMethodWithoutParams(model, nodeAtPosition, fix, error, isOldAST) |
|
|
|
|
this.addQuickFix(actions, error, model.uri, { |
|
|
|
|
id: fix.id, |
|
|
|
|
title: fix.title, |
|
|
|
@ -76,12 +48,7 @@ implements monaco.languages.CodeActionProvider |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
for (const fix of fixes) { |
|
|
|
|
if ( |
|
|
|
|
fix && |
|
|
|
|
nodeAtPosition && |
|
|
|
|
fix.nodeType !== nodeAtPosition.nodeType |
|
|
|
|
) |
|
|
|
|
continue |
|
|
|
|
if (fix && nodeAtPosition && fix.nodeType !== nodeAtPosition.nodeType) continue |
|
|
|
|
else |
|
|
|
|
this.addQuickFix(actions, error, model.uri, { |
|
|
|
|
title: fix.title, |
|
|
|
@ -106,12 +73,7 @@ implements monaco.languages.CodeActionProvider |
|
|
|
|
* @param uri model URI |
|
|
|
|
* @param fix details of quick fix to apply |
|
|
|
|
*/ |
|
|
|
|
addQuickFix( |
|
|
|
|
actions: monaco.languages.CodeAction[], |
|
|
|
|
error: monaco.editor.IMarkerData, |
|
|
|
|
uri: monaco.Uri, |
|
|
|
|
fix: Record<string, any> |
|
|
|
|
) { |
|
|
|
|
addQuickFix(actions: monaco.languages.CodeAction[], error: monaco.editor.IMarkerData, uri: monaco.Uri, fix: Record<string, any>) { |
|
|
|
|
const {id, title, range, text} = fix |
|
|
|
|
actions.push({ |
|
|
|
|
title, |
|
|
|
@ -122,7 +84,7 @@ implements monaco.languages.CodeActionProvider |
|
|
|
|
{ |
|
|
|
|
resource: uri, |
|
|
|
|
textEdit: {range, text}, |
|
|
|
|
versionId: id |
|
|
|
|
versionId: undefined |
|
|
|
|
} |
|
|
|
|
] |
|
|
|
|
}, |
|
|
|
@ -150,11 +112,7 @@ implements monaco.languages.CodeActionProvider |
|
|
|
|
// Get last function parameter node
|
|
|
|
|
const lastParamNode: Record<string, any> = paramNodes[paramNodes.length - 1] |
|
|
|
|
if (isOldAST) { |
|
|
|
|
const location: Record<string, any> = await this.props.plugin.call( |
|
|
|
|
'codeParser', |
|
|
|
|
'getLineColumnOfNode', |
|
|
|
|
lastParamNode |
|
|
|
|
) |
|
|
|
|
const location: Record<string, any> = await this.props.plugin.call('codeParser', 'getLineColumnOfNode', lastParamNode) |
|
|
|
|
// Get end location of last function parameter, it returns end column of parameter name
|
|
|
|
|
lastParamEndLoc = location.end |
|
|
|
|
fixLineNumber = lastParamEndLoc.line + 1 |
|
|
|
@ -164,24 +122,13 @@ implements monaco.languages.CodeActionProvider |
|
|
|
|
fixLineNumber = lastParamEndLoc.line |
|
|
|
|
} |
|
|
|
|
const lineContent: string = model.getLineContent(fixLineNumber) |
|
|
|
|
if (fix.id === 5 && lineContent.includes(' view ')) |
|
|
|
|
msg = lineContent.replace('view', 'pure') |
|
|
|
|
else if (isOldAST) |
|
|
|
|
msg = |
|
|
|
|
lineContent.substring(0, lastParamEndLoc.column + 2) + |
|
|
|
|
fix.message + |
|
|
|
|
lineContent.substring(lastParamEndLoc.column + 1, lineContent.length) |
|
|
|
|
if (fix.id === 5 && lineContent.includes(' view ')) msg = lineContent.replace('view', 'pure') |
|
|
|
|
else if (isOldAST) msg = lineContent.substring(0, lastParamEndLoc.column + 2) + fix.message + lineContent.substring(lastParamEndLoc.column + 1, lineContent.length) |
|
|
|
|
else |
|
|
|
|
msg = |
|
|
|
|
lineContent.substring( |
|
|
|
|
0, |
|
|
|
|
lastParamEndLoc.column + lastParamNode.name.length + 2 |
|
|
|
|
) + |
|
|
|
|
lineContent.substring(0, lastParamEndLoc.column + lastParamNode.name.length + 2) + |
|
|
|
|
fix.message + |
|
|
|
|
lineContent.substring( |
|
|
|
|
lastParamEndLoc.column + lastParamNode.name.length + 1, |
|
|
|
|
lineContent.length |
|
|
|
|
) |
|
|
|
|
lineContent.substring(lastParamEndLoc.column + lastParamNode.name.length + 1, lineContent.length) |
|
|
|
|
|
|
|
|
|
fix.range = { |
|
|
|
|
startLineNumber: fixLineNumber, |
|
|
|
@ -210,11 +157,7 @@ implements monaco.languages.CodeActionProvider |
|
|
|
|
): Promise<string> { |
|
|
|
|
let fixLineNumber: number, msg: string |
|
|
|
|
if (isOldAST) { |
|
|
|
|
const location: Record<string, any> = await this.props.plugin.call( |
|
|
|
|
'codeParser', |
|
|
|
|
'getLineColumnOfNode', |
|
|
|
|
nodeAtPosition |
|
|
|
|
) |
|
|
|
|
const location: Record<string, any> = await this.props.plugin.call('codeParser', 'getLineColumnOfNode', nodeAtPosition) |
|
|
|
|
fixLineNumber = location.start.line + 1 |
|
|
|
|
} else fixLineNumber = nodeAtPosition.loc.start.line |
|
|
|
|
|
|
|
|
@ -223,11 +166,7 @@ implements monaco.languages.CodeActionProvider |
|
|
|
|
|
|
|
|
|
if (fix.id === 5 && lineContent.includes(' view ')) { |
|
|
|
|
msg = lineContent.replace('view', 'pure') |
|
|
|
|
} else |
|
|
|
|
msg = |
|
|
|
|
lineContent.substring(0, i + 3) + |
|
|
|
|
fix.message + |
|
|
|
|
lineContent.substring(i + 3, lineContent.length) |
|
|
|
|
} else msg = lineContent.substring(0, i + 3) + fix.message + lineContent.substring(i + 3, lineContent.length) |
|
|
|
|
|
|
|
|
|
fix.range = { |
|
|
|
|
startLineNumber: fixLineNumber, |
|
|
|
|