beginning work on race condition bug

This commit is contained in:
Manindra Moharana
2014-02-02 00:36:33 +05:30
parent f36a21a00a
commit 6f0195534f
3 changed files with 136 additions and 6 deletions

View File

@@ -18,6 +18,7 @@
package processing.mode.experimental;
import static processing.mode.experimental.ExperimentalMode.log;
import static processing.mode.experimental.ExperimentalMode.log2;
import static processing.mode.experimental.ExperimentalMode.logE;
import java.awt.BorderLayout;
@@ -25,6 +26,7 @@ import java.awt.Color;
import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
@@ -108,7 +110,7 @@ public class CompletionPanel {
textarea.requestFocusInWindow();
popupMenu.show(textarea, location.x, textarea.getBaseline(0, 0)
+ location.y);
//log("Suggestion constructed" + System.nanoTime());
log("Suggestion shown: " + System.currentTimeMillis());
}
public boolean isVisible() {
@@ -205,7 +207,7 @@ public class CompletionPanel {
try {
String selectedSuggestion = ((CompletionCandidate) completionList
.getSelectedValue()).getCompletionString().substring(subWord.length());
logE(subWord+" <= subword,Inserting suggestion=> " + selectedSuggestion);
logE(subWord+" <= subword,Inserting suggestion=> " + selectedSuggestion + " Current sub: " + fetchPhrase());
textarea.getDocument().remove(insertionPosition-subWord.length(), subWord.length());
textarea.getDocument().insertString(insertionPosition-subWord.length(),
((CompletionCandidate) completionList
@@ -223,6 +225,7 @@ public class CompletionPanel {
else {
textarea.setCaretPosition(insertionPosition + selectedSuggestion.length());
}
log("Suggestion inserted: " + System.currentTimeMillis());
return true;
} catch (BadLocationException e1) {
e1.printStackTrace();
@@ -231,6 +234,120 @@ public class CompletionPanel {
}
return false;
}
private String fetchPhrase() {
TextArea ta = editor.ta;
int off = ta.getCaretPosition();
log2("off " + off);
if (off < 0)
return null;
int line = ta.getCaretLine();
if (line < 0)
return null;
String s = ta.getLineText(line);
log2("lin " + line);
/*
* if (s == null) return null; else if (s.length() == 0) return null;
*/
// else {
//log2(s + " len " + s.length());
int x = ta.getCaretPosition() - ta.getLineStartOffset(line) - 1, x2 = x + 1, x1 = x - 1;
if(x >= s.length() || x < 0)
return null; //TODO: Does this check cause problems? Verify.
log2(" x char: " + s.charAt(x));
//int xLS = off - getLineStartNonWhiteSpaceOffset(line);
String word = (x < s.length() ? s.charAt(x) : "") + "";
if (s.trim().length() == 1) {
// word = ""
// + (keyChar == KeyEvent.CHAR_UNDEFINED ? s.charAt(x - 1) : keyChar);
//word = (x < s.length()?s.charAt(x):"") + "";
word = word.trim();
if (word.endsWith("."))
word = word.substring(0, word.length() - 1);
return word;
}
// if (keyChar == KeyEvent.VK_BACK_SPACE || keyChar == KeyEvent.VK_DELETE)
// ; // accepted these keys
// else if (!(Character.isLetterOrDigit(keyChar) || keyChar == '_' || keyChar == '$'))
// return null;
int i = 0;
int closeB = 0;
while (true) {
i++;
//TODO: currently works on single line only. "a. <new line> b()" won't be detected
if (x1 >= 0) {
// if (s.charAt(x1) != ';' && s.charAt(x1) != ',' && s.charAt(x1) != '(')
if (Character.isLetterOrDigit(s.charAt(x1)) || s.charAt(x1) == '_'
|| s.charAt(x1) == '.' || s.charAt(x1) == ')' || s.charAt(x1) == ']') {
if (s.charAt(x1) == ')') {
word = s.charAt(x1--) + word;
closeB++;
while (x1 >= 0 && closeB > 0) {
word = s.charAt(x1) + word;
if (s.charAt(x1) == '(')
closeB--;
if (s.charAt(x1) == ')')
closeB++;
x1--;
}
}
else if (s.charAt(x1) == ']') {
word = s.charAt(x1--) + word;
closeB++;
while (x1 >= 0 && closeB > 0) {
word = s.charAt(x1) + word;
if (s.charAt(x1) == '[')
closeB--;
if (s.charAt(x1) == ']')
closeB++;
x1--;
}
}
else {
word = s.charAt(x1--) + word;
}
} else {
break;
}
} else {
break;
}
// if (x2 >= 0 && x2 < s.length()) {
// if (Character.isLetterOrDigit(s.charAt(x2)) || s.charAt(x2) == '_'
// || s.charAt(x2) == '$')
// word = word + s.charAt(x2++);
// else
// x2 = -1;
// } else
// x2 = -1;
// if (x1 < 0 )//&& x2 < 0
// break;
if (i > 200) {
// time out!
break;
}
}
// if (keyChar != KeyEvent.CHAR_UNDEFINED)
if (Character.isDigit(word.charAt(0)))
return null;
word = word.trim();
// if (word.endsWith("."))
// word = word.substring(0, word.length() - 1);
if(word.length() > 1)
//showSuggestionLater();
return word;
else return "";
//}
}
/**
* Hide the suggestion list