Development Adding a QuickFix - uqbar-project/wollok GitHub Wiki

Quick Fixes are a useful tool in order to accelerate development and learning to students.

image

If you want to develop a Quick Fix, you should take a look to WollokDslQuickfixProvider class in org.uqbar.project.wollok.ui.quickfix package (wollok.ui project).

There is also an important class: QuickFixUtils in the same package, which provides utilities for quickfixes. For example extension methods for common tasks when directly manipulating the document text, like adding a variable or defining a new method.

An example

In a method there is an invalid assignment to an unexistent variable. So, Quick Fix proposes several actions to fix this error.

  1. First of all, we have to catch error reported by Validator
@Fix(WollokDslValidator.CLASS_NAME_MUST_START_UPPERCASE)

Or dig into another kind of error, like unresolved references. It depends on each case.

  1. Then, propose a quick fix by adding a menu option with an action that handles the fix. For example, if you want to add a local variable to a test, suite, class, wko or unnamed object, you can do this:
// create local var
issueResolutionAcceptor.accept(issue, 'Create local variable', 'Create new local variable.', "variable.gif") [ e, context |
	val newVarName = xtextDocument.get(issue.offset, issue.length)
	val firstExpressionInContext = e.firstExpressionInContext
	context.insertBefore(firstExpressionInContext, VAR + " " + newVarName)
]

You can also hide quick fixes options depending on a certain element. For example, adding an instance variable is not an option if you are in an isolated test definition file:

val targetContext = target.getSelfContext
val hasMethodContainer = targetContext != null
...
// create instance var
if (hasMethodContainer) {
	issueResolutionAcceptor.accept(issue, 'Create instance variable', 'Create new instance variable.', "variable.gif") [ e, context |
	   val newVarName = xtextDocument.get(issue.offset, issue.length)
	   val declaringContext = e.declaringContext
	   val firstClassChild = declaringContext.eContents.head
	   context.insertBefore(firstClassChild, VAR + " " + newVarName)
        ]
}

See also

  • WMethodContainerExtensions
  • WollokModelExtensions

both in package org.uqbar.project.wollok.model, project org.uqbar.project.wollok

Further information

Please see Xtext online documentation