Skip to content

Commit

Permalink
Fixes #443 new quickfix for missing return
Browse files Browse the repository at this point in the history
  • Loading branch information
javierfernandes committed Nov 19, 2015
1 parent fa72ce6 commit 0036d07
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.uqbar.project.wollok.tests.interpreter

import org.uqbar.project.wollok.tests.interpreter.AbstractWollokInterpreterTestCase
import org.junit.Test
import org.uqbar.project.wollok.WollokDSLKeywords
import static org.uqbar.project.wollok.WollokConstants.*

/**
* Test cases for operators such as: +=, -=, etc.
Expand Down Expand Up @@ -46,11 +46,11 @@ class MultiOpAssignTestCase extends AbstractWollokInterpreterTestCase {
def void testMatches() {
// valid
#["+=", "-=", "*=", "/=", "%="].forEach[s|
assertTrue(s.matches(WollokDSLKeywords.MULTIOPS_REGEXP))
assertTrue(s.matches(MULTIOPS_REGEXP))
]
// not valid
#["++=", "+_", "a="].forEach[s|
assertFalse(s.matches(WollokDSLKeywords.MULTIOPS_REGEXP))
assertFalse(s.matches(MULTIOPS_REGEXP))
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.eclipse.xtext.ui.editor.quickfix.Fix
import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor
import org.eclipse.xtext.util.concurrent.IUnitOfWork
import org.eclipse.xtext.validation.Issue
import org.uqbar.project.wollok.WollokDSLKeywords
import org.uqbar.project.wollok.interpreter.WollokClassFinder
import org.uqbar.project.wollok.ui.Messages
import org.uqbar.project.wollok.validation.WollokDslValidator
Expand All @@ -29,19 +28,21 @@ import org.uqbar.project.wollok.wollokDsl.WVariableReference
import org.uqbar.project.wollok.wollokDsl.WollokDslFactory
import org.uqbar.project.wollok.wollokDsl.WollokDslPackage

import static org.uqbar.project.wollok.WollokDSLKeywords.*
import static org.uqbar.project.wollok.WollokConstants.*
import static org.uqbar.project.wollok.validation.WollokDslValidator.*

import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*
import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*
import static extension org.uqbar.project.wollok.ui.quickfix.QuickFixUtils.*

import static extension org.uqbar.project.wollok.utils.XTextExtensions.*
import org.uqbar.project.wollok.wollokDsl.WBlockExpression

/**
* Custom quickfixes.
*
* see http://www.eclipse.org/Xtext/documentation.html#quickfixes
*
* @author jfernandes
*/
class WollokDslQuickfixProvider extends DefaultQuickfixProvider {
@Inject
Expand Down Expand Up @@ -138,7 +139,7 @@ class WollokDslQuickfixProvider extends DefaultQuickfixProvider {
@Fix(METHOD_DOESNT_OVERRIDE_ANYTHING)
def removeOverrideKeyword(Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, 'Remove override keyword', 'Remove override keyword.', null) [ e, it |
xtextDocument.deleteToken(e, WollokDSLKeywords.OVERRIDE)
xtextDocument.deleteToken(e, OVERRIDE)
]
}

Expand Down Expand Up @@ -260,11 +261,20 @@ class WollokDslQuickfixProvider extends DefaultQuickfixProvider {
val ifE = e as WIfExpression
var inlineResult = if (ifE.then.isReturnTrue) ifE.condition.sourceCode else ("!(" + ifE.condition.sourceCode + ")")
if (ifE.then.hasReturnWithValue) {
inlineResult = "return " + inlineResult
inlineResult = RETURN + " " + inlineResult
}
xtextDocument.replaceWith(e, inlineResult)
]
}

@Fix(RETURN_FORGOTTEN)
def prependReturn(Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, 'Prepend "return"', 'Adds a "return" keyword', null) [ e, it |
val method = e as WMethodDeclaration
val body = (method.expression as WBlockExpression)
insertBefore(body.expressions.last, RETURN + " ")
]
}

// ************************************************
// ** unresolved ref to "elements" quick fixes
Expand All @@ -275,14 +285,14 @@ class WollokDslQuickfixProvider extends DefaultQuickfixProvider {
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.block.expressions.head
context.insertBefore(firstExpressionInContext, "var " + newVarName)
context.insertBefore(firstExpressionInContext, VAR + " " + newVarName)
]

// create instance var
issueResolutionAcceptor.accept(issue, 'Create instance variable', 'Create new instance variable.', "variable.gif") [ e, context |
val newVarName = xtextDocument.get(issue.offset, issue.length)
val firstClassChild = (e as WExpression).method.declaringContext.eContents.head
context.insertBefore(firstClassChild, "var " + newVarName)
context.insertBefore(firstClassChild, VAR + " " + newVarName)
]

// create parameter
Expand All @@ -298,7 +308,7 @@ class WollokDslQuickfixProvider extends DefaultQuickfixProvider {
issueResolutionAcceptor.accept(issue, 'Create new class', 'Create a new class definition.', "class.png") [ e, context |
val newClassName = xtextDocument.get(issue.offset, issue.length)
val container = (e as WExpression).method.declaringContext
context.xtextDocument.replace(container.after, 0, "\nclass " + newClassName + " {\n}\n")
context.xtextDocument.replace(container.after, 0, "\n" + CLASS + newClassName + " {\n}\n")
]

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package org.uqbar.project.wollok

/**
* Contains language keywords defined in the grammar
* but then used by the interpreter or any other processing (like quickfixes).
*
* Avoids hardcoding strings all over the places.
* So that if we decide to change the grammar sintax we can just change here.
*
* There's probably a way to get this via xtext but I'm not sure how
*
* @author jfernandes
*/
Expand All @@ -11,6 +18,19 @@ class WollokConstants {
public static val PROGRAM_EXTENSION = "wpgm"
public static val TEST_EXTENSION = "wtest"

// grammar elements here for being used in quickfixes, validators, and
// any code that generates wollok code

public static val OPMULTIASSIGN = #['+=', '-=', '*=', '/=', '%=', '<<=', '>>=']

public static val THIS = "this"
public static val METHOD = "method"
public static val VAR = "var"
public static val OVERRIDE = "override"
public static val RETURN = "return"
public static val CLASS = "class"

public static val MULTIOPS_REGEXP = "[+\\-*/%]="

public static def isMultiOpAssignment(String operator) { operator.matches(MULTIOPS_REGEXP) }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import org.uqbar.project.wollok.wollokDsl.WVariableDeclaration
import org.uqbar.project.wollok.wollokDsl.WVariableReference
import org.uqbar.project.wollok.wollokDsl.WollokDslPackage

import static extension org.uqbar.project.wollok.WollokDSLKeywords.*
import static extension org.uqbar.project.wollok.WollokConstants.*
import static extension org.uqbar.project.wollok.interpreter.context.EvaluationContextExtensions.*
import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*
import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.uqbar.project.wollok.wollokDsl.WMethodContainer
import org.uqbar.project.wollok.wollokDsl.WMethodDeclaration
import org.uqbar.project.wollok.wollokDsl.WVariableDeclaration

import static org.uqbar.project.wollok.WollokDSLKeywords.*
import static org.uqbar.project.wollok.WollokConstants.*

import static extension org.uqbar.project.wollok.interpreter.context.EvaluationContextExtensions.*
import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import org.uqbar.project.wollok.wollokDsl.WVariableReference
import static org.uqbar.project.wollok.Messages.*
import static org.uqbar.project.wollok.wollokDsl.WollokDslPackage.Literals.*

import static extension org.uqbar.project.wollok.WollokDSLKeywords.*
import static extension org.uqbar.project.wollok.WollokConstants.*
import static extension org.uqbar.project.wollok.model.WBlockExtensions.*
import static extension org.uqbar.project.wollok.model.WEvaluationExtension.*
import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*
Expand Down

0 comments on commit 0036d07

Please sign in to comment.