@@ -1095,9 +1095,10 @@ public void actionPerformed(ActionEvent e) {
1095
1095
item = newJMenuItemShift (_ ("Find in Reference" ), 'F' );
1096
1096
item .addActionListener (new ActionListener () {
1097
1097
public void actionPerformed (ActionEvent e ) {
1098
- if (textarea .isSelectionActive ()) {
1099
- handleFindReference ();
1100
- }
1098
+ // if (textarea.isSelectionActive()) {
1099
+ // handleFindReference();
1100
+ // }
1101
+ handleFindReference ();
1101
1102
}
1102
1103
});
1103
1104
menu .add (item );
@@ -1830,25 +1831,58 @@ protected void handleIndentOutdent(boolean indent) {
1830
1831
stopCompoundEdit ();
1831
1832
}
1832
1833
1833
-
1834
- protected void handleFindReference () {
1835
- String text = textarea .getSelectedText ().trim ();
1836
-
1837
- if (text .length () == 0 ) {
1838
- statusNotice (_ ("First select a word to find in the reference." ));
1839
-
1840
- } else {
1841
- String referenceFile = PdeKeywords .getReference (text );
1842
- //System.out.println("reference file is " + referenceFile);
1843
- if (referenceFile == null ) {
1844
- statusNotice (
1845
- I18n .format (_ ("No reference available for \" {0}\" " ), text )
1846
- );
1847
- } else {
1848
- Base .showReference (I18n .format (_ ("{0}.html" ), referenceFile ));
1849
- }
1850
- }
1851
- }
1834
+ protected String getCurrentKeyword () {
1835
+ String text = "" ;
1836
+ if (textarea .getSelectedText () != null )
1837
+ text = textarea .getSelectedText ().trim ();
1838
+
1839
+ try {
1840
+ int current = textarea .getCaretPosition ();
1841
+ int startOffset = 0 ;
1842
+ int endIndex = current ;
1843
+ String tmp = textarea .getDocument ().getText (current , 1 );
1844
+ // TODO probably a regexp that matches Arduino lang special chars
1845
+ // already exists.
1846
+ String regexp = "[\\ s\\ n();\\ \\ .!='\\ [\\ ]{}]" ;
1847
+
1848
+ while (!tmp .matches (regexp )) {
1849
+ endIndex ++;
1850
+ tmp = textarea .getDocument ().getText (endIndex , 1 );
1851
+ }
1852
+ // For some reason document index start at 2.
1853
+ // if( current - start < 2 ) return;
1854
+
1855
+ tmp = "" ;
1856
+ while (!tmp .matches (regexp )) {
1857
+ startOffset ++;
1858
+ if (current - startOffset < 0 ) {
1859
+ tmp = textarea .getDocument ().getText (0 , 1 );
1860
+ break ;
1861
+ } else
1862
+ tmp = textarea .getDocument ().getText (current - startOffset , 1 );
1863
+ }
1864
+ startOffset --;
1865
+
1866
+ int length = endIndex - current + startOffset ;
1867
+ text = textarea .getDocument ().getText (current - startOffset , length );
1868
+
1869
+ } catch (BadLocationException bl ) {
1870
+ bl .printStackTrace ();
1871
+ } finally {
1872
+ return text ;
1873
+ }
1874
+ }
1875
+
1876
+ protected void handleFindReference () {
1877
+ String text = getCurrentKeyword ();
1878
+
1879
+ String referenceFile = PdeKeywords .getReference (text );
1880
+ if (referenceFile == null ) {
1881
+ statusNotice (I18n .format (_ ("No reference available for \" {0}\" " ), text ));
1882
+ } else {
1883
+ Base .showReference (I18n .format (_ ("{0}.html" ), referenceFile ));
1884
+ }
1885
+ }
1852
1886
1853
1887
1854
1888
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@@ -2775,16 +2809,15 @@ public void show(Component component, int x, int y) {
2775
2809
copyItem .setEnabled (true );
2776
2810
discourseItem .setEnabled (true );
2777
2811
2778
- String sel = textarea .getSelectedText ().trim ();
2779
- referenceFile = PdeKeywords .getReference (sel );
2780
- referenceItem .setEnabled (referenceFile != null );
2781
-
2782
2812
} else {
2783
2813
cutItem .setEnabled (false );
2784
2814
copyItem .setEnabled (false );
2785
2815
discourseItem .setEnabled (false );
2786
- referenceItem .setEnabled (false );
2787
2816
}
2817
+
2818
+ referenceFile = PdeKeywords .getReference (getCurrentKeyword ());
2819
+ referenceItem .setEnabled (referenceFile != null );
2820
+
2788
2821
super .show (component , x , y );
2789
2822
}
2790
2823
}
0 commit comments