@@ -1073,9 +1073,10 @@ public void actionPerformed(ActionEvent e) {
1073
1073
item = newJMenuItemShift (_ ("Find in Reference" ), 'F' );
1074
1074
item .addActionListener (new ActionListener () {
1075
1075
public void actionPerformed (ActionEvent e ) {
1076
- if (textarea .isSelectionActive ()) {
1077
- handleFindReference ();
1078
- }
1076
+ // if (textarea.isSelectionActive()) {
1077
+ // handleFindReference();
1078
+ // }
1079
+ handleFindReference ();
1079
1080
}
1080
1081
});
1081
1082
menu .add (item );
@@ -1809,24 +1810,51 @@ protected void handleIndentOutdent(boolean indent) {
1809
1810
}
1810
1811
1811
1812
1812
- protected void handleFindReference () {
1813
- String text = textarea .getSelectedText ().trim ();
1814
-
1815
- if (text .length () == 0 ) {
1816
- statusNotice (_ ("First select a word to find in the reference." ));
1817
-
1818
- } else {
1819
- String referenceFile = PdeKeywords .getReference (text );
1820
- //System.out.println("reference file is " + referenceFile);
1821
- if (referenceFile == null ) {
1822
- statusNotice (
1823
- I18n .format (_ ("No reference available for \" {0}\" " ), text )
1824
- );
1825
- } else {
1826
- Base .showReference (I18n .format (_ ("{0}.html" ), referenceFile ));
1827
- }
1828
- }
1829
- }
1813
+ protected void handleFindReference () {
1814
+ String text = "" ;
1815
+ if ( textarea .getSelectedText () != null )
1816
+ text = textarea .getSelectedText ().trim ();
1817
+
1818
+ try {
1819
+ int current = textarea .getCaretPosition ();
1820
+ int startOffset = 0 ;
1821
+ int endIndex = current ;
1822
+ String tmp = textarea .getDocument ().getText (current ,1 );
1823
+ // TODO probably a regexp that matches Arduino lang special chars already exists.
1824
+ String regexp = "[\\ s\\ n();\\ \\ .!='\\ [\\ ]{}]" ;
1825
+
1826
+ while (!tmp .matches (regexp )) {
1827
+ endIndex ++;
1828
+ tmp = textarea .getDocument ().getText (endIndex ,1 );
1829
+ }
1830
+ // For some reason document index start at 2.
1831
+ //if( current - start < 2 ) return;
1832
+
1833
+ tmp = "" ;
1834
+ while (!tmp .matches (regexp )) {
1835
+ startOffset ++;
1836
+ if ( current - startOffset < 0 ) {
1837
+ tmp = textarea .getDocument ().getText (0 , 1 );
1838
+ break ;
1839
+ }
1840
+ else
1841
+ tmp = textarea .getDocument ().getText (current - startOffset , 1 );
1842
+ }
1843
+ startOffset --;
1844
+
1845
+ int length = endIndex - current + startOffset ;
1846
+ text = textarea .getDocument ().getText (current - startOffset , length );
1847
+ } catch (BadLocationException bl ) {
1848
+ bl .printStackTrace ();
1849
+ }
1850
+
1851
+ String referenceFile = PdeKeywords .getReference (text );
1852
+ if (referenceFile == null ) {
1853
+ statusNotice (I18n .format (_ ("No reference available for \" {0}\" " ), text ));
1854
+ } else {
1855
+ Base .showReference (I18n .format (_ ("{0}.html" ), referenceFile ));
1856
+ }
1857
+ }
1830
1858
1831
1859
1832
1860
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0 commit comments