Permalink
Browse files

implemented complete/execute actions

  • Loading branch information...
1 parent 3027877 commit e13ce77287a361cbb45740712981969f4b2642f7 @sickill committed Oct 4, 2009
@@ -3,15 +3,86 @@ package net.sickill.finishhim
import org.gjt.sp.jedit.View
import org.gjt.sp.jedit.Buffer
import org.gjt.sp.jedit.textarea.JEditTextArea
+import org.gjt.sp.jedit.textarea.Selection
import org.gjt.sp.util.Log
+import org.gjt.sp.jedit.TextUtilities
object FinishHimExecutor {
+ var completing = false
+ var invoked = false
+ var wordList: List[String] = List()
+ var nextWordIndex = 0
+ var buffer: Buffer = null
+ var textArea: JEditTextArea = null
+ var caret = 0
+ var prefixLength = 0
+ var suggestedWordLength = 0
+ var caretLine = 0
+
def execute(view: View) = {
- Log.log(Log.DEBUG, this, "executing...")
- val buffer = view.getBuffer()
- val textArea = view.getTextArea()
- val caret = textArea.getCaretPosition()
- buffer.insert(caret, ":)")
- Log.log(Log.DEBUG, this, "done!")
+ invoked = true
+ log("executing...")
+ if (completing) {
+ log("continuing completion...")
+ complete(view)
+ } else {
+ log("starting completion...")
+ if (setup(view)) {
+ complete(view)
+ }
+ }
+ }
+
+ def setup(view: View) : Boolean = {
+ nextWordIndex = 0
+ buffer = view.getBuffer()
+ textArea = view.getTextArea()
+ caret = textArea.getCaretPosition()
+ caretLine = textArea.getCaretLine()
+ val prefix = getPrefix()
+ if (prefix != null) {
+ log("FinishHim: found prefix: " + prefix)
+ completing = true
+ prefixLength = prefix.length()
+ suggestedWordLength = prefixLength
+ buildWordList(prefix, buffer.getText(0, buffer.getLength()))
+ true
+ } else {
+ log("FinishHim: empty prefix, leaving")
+ false
+ }
+ }
+
+ def buildWordList(prefix: String, bufferText: String) = {
+ log("FinishHim: buildWordList")
+ wordList = List(prefix + "l", prefix + "ls", prefix + "bar", prefix + "bar2000")
+ }
+
+ def getPrefix() : String = {
+ log("FinishHim: getPrefix")
+ val line = buffer.getLineSegment(caretLine)
+ val dot = caret - buffer.getLineStartOffset(caretLine)
+ if (dot == 0) return null
+ val ch = line.charAt(dot-1)
+ if (!Character.isLetterOrDigit(ch)) return null
+ val wordStartPos = TextUtilities.findWordStart(line, dot-1, "")
+ val prefix = line.subSequence(wordStartPos, dot)
+ if (prefix.length() == 0) return null
+ prefix.toString()
+ }
+
+ def complete(view: View) = {
+ log("FinishHim: complete")
+ val nextWord = wordList(nextWordIndex)
+ textArea.setSelection(new Selection.Range(caret, caret - prefixLength + suggestedWordLength))
+ textArea.replaceSelection(nextWord.substring(prefixLength))
+ suggestedWordLength = nextWord.length()
+ nextWordIndex += 1
+ if (nextWordIndex >= wordList.size) nextWordIndex = 0
+ log("done!")
+ }
+
+ def log(msg: String) = {
+ Log.log(Log.DEBUG, this, msg)
}
}
@@ -2,11 +2,21 @@ package net.sickill.finishhim
import org.gjt.sp.jedit.View
import org.gjt.sp.jedit.gui.DefaultInputHandler
-// import org.gjt.sp.jedit.gui.KeyEventTranslator.Key
+import org.gjt.sp.jedit.gui.KeyEventTranslator
+import org.gjt.sp.util.Log
class FinishHimInputHandler(view: View) extends DefaultInputHandler(view) {
- // def handleKey(keyStroke: KeyEventTranslator.Key, dryRun: boolean) : boolean = {
- // Log.log(Log.DEBUG, this, "pressed " + keyStroke)
- // super(keyStroke, dryRun)
- // }
+ val defaultInputHandler = view.getInputHandler()
+
+ override def handleKey(keyStroke: KeyEventTranslator.Key, dryRun: Boolean) : Boolean = {
+ val r = defaultInputHandler.handleKey(keyStroke, dryRun)
+ if (FinishHimExecutor.invoked) {
+ FinishHimExecutor.invoked = false
+ Log.log(Log.DEBUG, this, "invoked completion action")
+ } else if (FinishHimExecutor.completing) {
+ FinishHimExecutor.completing = false
+ Log.log(Log.DEBUG, this, "finished completion by pressing " + keyStroke)
+ }
+ r
+ }
}
@@ -1,7 +1,52 @@
package net.sickill.finishhim
+import org.gjt.sp.jedit.jEdit
+import org.gjt.sp.jedit.EBMessage
import org.gjt.sp.jedit.EditPlugin
+import org.gjt.sp.util.Log
+import org.gjt.sp.jedit.View
+import org.gjt.sp.jedit.msg.ViewUpdate
class FinishHimPlugin extends EditPlugin {
+ override def start = {
+ for(view <- jEdit.getViews()) {
+ initView(view);
+ }
+ }
+
+ override def stop = {
+ for(view <- jEdit.getViews()) {
+ unInitView(view);
+ }
+ }
+
+ def initView(view: View) = {
+ Log.log(Log.DEBUG, this, "initView("+view+")");
+ val ih = view.getInputHandler()
+ if (!ih.isInstanceOf[FinishHimInputHandler]) {
+ view.setInputHandler(new FinishHimInputHandler(view))
+ }
+ }
+
+ def unInitView(view: View) = {
+ Log.log(Log.DEBUG, this, "unInitView("+view+")");
+ val ih = view.getInputHandler()
+ if (ih.isInstanceOf[FinishHimInputHandler]) {
+ view.setInputHandler(ih.asInstanceOf[FinishHimInputHandler].defaultInputHandler)
+ }
+ }
+
+ def handleMessage(msg: EBMessage) = {
+ if (msg.isInstanceOf[ViewUpdate]) {
+ val vu = msg.asInstanceOf[ViewUpdate];
+ if (vu.getWhat() == ViewUpdate.CREATED) {
+ initView(vu.getView())
+ } else if (vu.getWhat() == ViewUpdate.CLOSED) {
+ unInitView(vu.getView());
+ }
+ // } else if (msg.isInstanceOf[PropertiesChanged]) {
+ // loadAccents();
+ }
+ } //}}}
}

0 comments on commit e13ce77

Please sign in to comment.