- Moved code guessing current keyword in its function

- Find-in-reference disabled in right-click popup menu unless
appropriate text is selected (#1014)
This commit is contained in:
s17t.net 2012-08-30 00:30:33 +02:00
parent 222d51e383
commit 2ca5d3e056
1 changed files with 28 additions and 23 deletions

View File

@ -1809,44 +1809,50 @@ public class Editor extends JFrame implements RunnerListener {
stopCompoundEdit(); stopCompoundEdit();
} }
protected String getCurrentKeyword() {
protected void handleFindReference() {
String text = ""; String text = "";
if( textarea.getSelectedText() != null ) if (textarea.getSelectedText() != null)
text = textarea.getSelectedText().trim(); text = textarea.getSelectedText().trim();
try { try {
int current = textarea.getCaretPosition(); int current = textarea.getCaretPosition();
int startOffset = 0; int startOffset = 0;
int endIndex = current; int endIndex = current;
String tmp = textarea.getDocument().getText(current,1); String tmp = textarea.getDocument().getText(current, 1);
// TODO probably a regexp that matches Arduino lang special chars already exists. // TODO probably a regexp that matches Arduino lang special chars
// already exists.
String regexp = "[\\s\\n();\\\\.!='\\[\\]{}]"; String regexp = "[\\s\\n();\\\\.!='\\[\\]{}]";
while(!tmp.matches(regexp)) { while (!tmp.matches(regexp)) {
endIndex++; endIndex++;
tmp = textarea.getDocument().getText(endIndex,1); tmp = textarea.getDocument().getText(endIndex, 1);
} }
// For some reason document index start at 2. // For some reason document index start at 2.
//if( current - start < 2 ) return; // if( current - start < 2 ) return;
tmp = ""; tmp = "";
while(!tmp.matches(regexp)) { while (!tmp.matches(regexp)) {
startOffset++; startOffset++;
if( current - startOffset < 0) { if (current - startOffset < 0) {
tmp = textarea.getDocument().getText(0, 1); tmp = textarea.getDocument().getText(0, 1);
break; break;
} } else
else
tmp = textarea.getDocument().getText(current - startOffset, 1); tmp = textarea.getDocument().getText(current - startOffset, 1);
} }
startOffset--; startOffset--;
int length = endIndex - current + startOffset; int length = endIndex - current + startOffset;
text = textarea.getDocument().getText(current - startOffset, length); text = textarea.getDocument().getText(current - startOffset, length);
} catch (BadLocationException bl ) {
bl.printStackTrace(); } catch (BadLocationException bl) {
bl.printStackTrace();
} finally {
return text;
} }
}
protected void handleFindReference() {
String text = getCurrentKeyword();
String referenceFile = PdeKeywords.getReference(text); String referenceFile = PdeKeywords.getReference(text);
if (referenceFile == null) { if (referenceFile == null) {
@ -2781,16 +2787,15 @@ public class Editor extends JFrame implements RunnerListener {
copyItem.setEnabled(true); copyItem.setEnabled(true);
discourseItem.setEnabled(true); discourseItem.setEnabled(true);
String sel = textarea.getSelectedText().trim();
referenceFile = PdeKeywords.getReference(sel);
referenceItem.setEnabled(referenceFile != null);
} else { } else {
cutItem.setEnabled(false); cutItem.setEnabled(false);
copyItem.setEnabled(false); copyItem.setEnabled(false);
discourseItem.setEnabled(false); discourseItem.setEnabled(false);
referenceItem.setEnabled(false);
} }
referenceFile = PdeKeywords.getReference(getCurrentKeyword());
referenceItem.setEnabled(referenceFile != null);
super.show(component, x, y); super.show(component, x, y);
} }
} }