Skip to content

Commit

Permalink
Use ScalaSourceFile as dependency in QuickAssist
Browse files Browse the repository at this point in the history
This has the advantage that no JDT API needs to be used for this case.
Another change is that no problems are computed anymore when no quick
assists are found.
  • Loading branch information
kiritsuku committed Oct 4, 2014
1 parent fbd9e73 commit d903f45
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.eclipse.ui.IEditorInput
import org.scalaide.core.quickassist.BasicCompletionProposal
import org.scalaide.core.quickassist.InvocationContext
import org.scalaide.util.eclipse.EditorUtils
import org.scalaide.core.internal.jdt.model.ScalaSourceFile

/**
* Referenced in the plugins.xml. This handler gets called when an user asks for
Expand Down Expand Up @@ -75,20 +76,21 @@ final class QuickAssistProcessor(input: IEditorInput, id: String) extends IQuick
import QuickAssistProcessor._

override def computeQuickAssistProposals(ctx: IQuickAssistInvocationContext): Array[ICompletionProposal] = {
val cu = JavaUI.getWorkingCopyManager.getWorkingCopy(input)
val model = JavaUI.getDocumentProvider.getAnnotationModel(input)

import collection.JavaConverters._
val iter = model.getAnnotationIterator.asScala
val problems = (iter foldLeft IndexedSeq[ProblemLocation]()) {
case (ps, a: Annotation with IJavaAnnotation) =>
val pos = model.getPosition(a)
if (isInside(ctx.getOffset, pos.offset, pos.offset+pos.length))
ps :+ new ProblemLocation(pos.offset, pos.length, a)
else
def problems = {
import collection.JavaConverters._
val model = JavaUI.getDocumentProvider.getAnnotationModel(input)
val iter = model.getAnnotationIterator.asScala

(iter foldLeft IndexedSeq[ProblemLocation]()) {
case (ps, a: Annotation with IJavaAnnotation) =>
val pos = model.getPosition(a)
if (isInside(ctx.getOffset, pos.offset, pos.offset+pos.length))
ps :+ new ProblemLocation(pos.offset, pos.length, a)
else
ps
case (ps, _) =>
ps
case (ps, _) =>
ps
}
}

val assists =
Expand All @@ -97,14 +99,20 @@ final class QuickAssistProcessor(input: IEditorInput, id: String) extends IQuick
else
QuickAssists.find(_.id == id).toSeq

val ictx = InvocationContext(cu, ctx.getOffset, ctx.getLength, problems)
val proposals = assists flatMap (_ withInstance (_ compute ictx))
val sorted = proposals.flatten.sortBy(-_.getRelevance())
JavaUI.getWorkingCopyManager.getWorkingCopy(input) match {
case ssf: ScalaSourceFile if assists.nonEmpty =>
val ictx = InvocationContext(ssf, ctx.getOffset, ctx.getLength, problems)
val proposals = assists flatMap (_ withInstance (_ compute ictx))
val sorted = proposals.flatten.sortBy(-_.getRelevance())

if (sorted.isEmpty)
Array(NoProposals)
else
sorted.toArray

if (sorted.isEmpty)
Array(NoProposals)
else
sorted.toArray
case _ =>
Array(NoProposals)
}
}

override def canAssist(ctx: IQuickAssistInvocationContext): Boolean = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,22 @@ class ScalaQuickAssistProcessor extends QuickAssist with HasLogger {
import ScalaQuickAssistProcessor._

override def compute(ctx: InvocationContext): Seq[BasicCompletionProposal] = {
val (start, len) = (ctx.selectionStart, ctx.selectionLength)
val (start, len, ssf) = (ctx.selectionStart, ctx.selectionLength, ctx.sourceFile)

ctx.compilationUnit match {
case ssf: ScalaSourceFile =>
import EditorUtils._
val assists = openEditorAndApply(ssf) { editor =>
val corrections = getAnnotationsAtOffset(editor, start) flatMap {
case (ann, pos) =>
suggestAssist(ann.getText, pos)
}
corrections.toArray.distinct
}
import EditorUtils._
val assists = openEditorAndApply(ssf) { editor =>
val corrections = getAnnotationsAtOffset(editor, start) flatMap {
case (ann, pos) =>
suggestAssist(ann.getText, pos)
}
corrections.toArray.distinct
}

val allAssists = ExplicitReturnType.suggestsFor(ssf, start).toArray ++
ImplAbstractMembers.suggestsFor(ssf, start) ++ assists ++
ExtractionProposal.getQuickAssistProposals(ssf, start, start + len)
val allAssists = ExplicitReturnType.suggestsFor(ssf, start).toArray ++
ImplAbstractMembers.suggestsFor(ssf, start) ++ assists ++
ExtractionProposal.getQuickAssistProposals(ssf, start, start + len)

allAssists.toSeq.asInstanceOf[Seq[BasicCompletionProposal]]
case _ => Nil
}
allAssists.toSeq.asInstanceOf[Seq[BasicCompletionProposal]]
}

private def suggestAssist(problemMessage: String, location: Position): Seq[IJavaCompletionProposal] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,31 @@ class ScalaQuickFixProcessor extends QuickAssist with HasLogger {
private val TypeMismatchError = """type mismatch;\s*found\s*: (\S*)\s*required: (.*)""".r

override def compute(ctx: InvocationContext): Seq[BasicCompletionProposal] = {
ctx.compilationUnit match {
case ssf: ScalaSourceFile => {
val editor = JavaUI.openInEditor(ssf)
var corrections: List[IJavaCompletionProposal] = Nil
for (location <- ctx.problemLocations)
for ((ann, pos) <- EditorUtils.getAnnotationsAtOffset(editor, location.getOffset)) {
val importFix = suggestImportFix(ssf, ann.getText)
val createClassFix = suggestCreateClassFix(ssf, ann.getText)

// compute all possible type mismatch quick fixes
val document = (editor.asInstanceOf[ITextEditor]).getDocumentProvider().getDocument(editor.getEditorInput())
val typeMismatchFix = suggestTypeMismatchFix(document, ann.getText, pos)

val createMethodFix = suggestCreateMethodFix(ssf, ann.getText, pos)
val changeMethodCase = suggestChangeMethodCase(ssf, ann.getText, pos)

// concatenate lists of found quick fixes
corrections = corrections ++
importFix ++
typeMismatchFix ++
createClassFix ++
createMethodFix ++
changeMethodCase

}
corrections.distinct.asInstanceOf[Seq[BasicCompletionProposal]]
}
val ssf = ctx.sourceFile
val editor = JavaUI.openInEditor(ssf)
var corrections: List[IJavaCompletionProposal] = Nil
for (location <- ctx.problemLocations)
for ((ann, pos) <- EditorUtils.getAnnotationsAtOffset(editor, location.getOffset)) {
val importFix = suggestImportFix(ssf, ann.getText)
val createClassFix = suggestCreateClassFix(ssf, ann.getText)

// compute all possible type mismatch quick fixes
val document = (editor.asInstanceOf[ITextEditor]).getDocumentProvider().getDocument(editor.getEditorInput())
val typeMismatchFix = suggestTypeMismatchFix(document, ann.getText, pos)

val createMethodFix = suggestCreateMethodFix(ssf, ann.getText, pos)
val changeMethodCase = suggestChangeMethodCase(ssf, ann.getText, pos)

// concatenate lists of found quick fixes
corrections = corrections ++
importFix ++
typeMismatchFix ++
createClassFix ++
createMethodFix ++
changeMethodCase

case _ => Nil
}
}
corrections.distinct.asInstanceOf[Seq[BasicCompletionProposal]]
}

private def suggestChangeMethodCase(cu: ICompilationUnit, problemMessage : String, pos: Position): List[IJavaCompletionProposal] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.scalaide.core.quickassist

import org.eclipse.jdt.core.ICompilationUnit
import org.eclipse.jdt.ui.text.java.IProblemLocation
import org.scalaide.core.internal.jdt.model.ScalaSourceFile

trait QuickAssist {
def compute(ctx: InvocationContext): Seq[BasicCompletionProposal]
}

case class InvocationContext(
compilationUnit: ICompilationUnit,
sourceFile: ScalaSourceFile,
selectionStart: Int,
selectionLength: Int,
problemLocations: Seq[IProblemLocation]
Expand Down

0 comments on commit d903f45

Please sign in to comment.