1 package net.sf.plb4jedit.plb;
2
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.util.Vector;
6
7 import org.gjt.sp.jedit.*;
8 import org.gjt.sp.jedit.io.VFSManager;
9 import org.gjt.sp.jedit.textarea.JEditTextArea;
10 import org.gjt.sp.util.Log;
11
12 import sidekick.SideKickParsedData;
13 import sidekick.SideKickPlugin;
14
15 /***
16 * jEdit plugin to add Plb-specific enhancements to jEdit:
17 * <ul>
18 * <li>SideKick structure view</li>
19 * <li>PlbParser, which gives "goto variable/label declaration" functionality</li>
20 * </ul>
21 *
22 * Plans/TODO for jEdit 4.2. Port to new plugin architecture. Use new version of SideKick-Plugin
23 * which gives easy to perform code-completion.
24 *
25 * @author Peter Schaefer
26 */
27 public class PlbPlugin extends EBPlugin {
28
29 /*** prefix to lookup properties in Plb.props*/
30 public final static String PROPERTY_PREFIX = "plugin.Plb.";
31 /*** singletion PlbParser instance for SideKick-Plugin */
32 public static final PlbParser PLB_PARSER_INSTANCE = new PlbParser("plb");
33 /*** jEdit plugin architecture: called on start-up to initialize plugin */
34 public void start() {
35 SideKickPlugin.registerParser(PLB_PARSER_INSTANCE);
36 }
37
38 /*** jEdit plugin architecture: called on shutdown */
39 public void stop() {
40 SideKickPlugin.unregisterParser(PLB_PARSER_INSTANCE);
41 }
42
43 /***
44 * creates Plugin menu entries from properties in Plb.props:
45 * plb.menu.label=Plb
46 * plb.menu=Plb - plb.open-variable-declaration plb.goto-label
47 *
48 * @param menuItems param
49 */
50 public void createMenuItems(Vector menuItems) {
51 menuItems.addElement(GUIUtilities.loadMenu("plb.menu"));
52 }
53
54 /***
55 * jEdit plugin architecture: handle messages (e.g. buffer save, etc)
56 */
57 public void handleMessage(EBMessage msg) {
58 }
59
60 /***
61 * get current Word from the textarea of the parameter view.
62 * recognizes $ and # as belonging to a word
63 * @param view to handle
64 * @return the word where the cursor stands in the textarea
65 */
66 private static String getCurrentWord(View view) {
67 JEditTextArea textarea = view.getTextArea();
68 int line = textarea.getCaretLine();
69 int lineStart = textarea.getLineStartOffset(line);
70 int offset = textarea.getCaretPosition() - lineStart;
71
72 if (textarea.getLineLength(line) == 0)
73 return null;
74
75 String lineText = textarea.getLineText(line);
76 String noWordSep = view.getBuffer().getStringProperty("noWordSep");
77 if (noWordSep.indexOf("$") == -1) {
78 noWordSep += "$";
79 }
80 if (noWordSep.indexOf("#") == -1) {
81 noWordSep += "#";
82 }
83 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), "noWordSep=" + noWordSep);
84
85 if (offset == textarea.getLineLength(line))
86 offset--;
87
88 int wordStart =
89 TextUtilities.findWordStart(lineText, offset, noWordSep);
90 int wordEnd =
91 TextUtilities.findWordEnd(lineText, offset + 1, noWordSep);
92 return lineText.substring(wordStart, wordEnd);
93 }
94
95 /***
96 * opens the variable definition in a new buffer.
97 * The lookup is performed with the word where the cursor stands.
98 * If nothing is found or on errors stati are shown in the status bar.
99 *
100 * Is linked with the action open-variable-declaration, see actions.xml.
101 *
102 */
103 public static void openVariableDeclaration() {
104 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), "openVariableDeclaration()");
105 View view = jEdit.getActiveView();
106 String word = getCurrentWord(view);
107 if (word == null || word.length() == 0) {
108 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), " no word found");
109 return;
110 }
111 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), " search for " + word);
112 ResourceManager resource = getResource(view);
113 if (resource == null) {
114 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), " resource null ");
115 view.getStatus().setMessage("Parse buffer first");
116 return;
117 }
118
119 Variable var = resource.searchVariable(word);
120
121 if (var == null) {
122 view.getStatus().setMessage(word + " not found");
123 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), " nothing found");
124 return;
125 }
126 openBuffer(view,var.getSource(),var.getLineno()-1);
127 }
128
129 /***
130 * goes to the position where the label under the cursor is defined.
131 * Opens this position in a new buffer. If nothing is found or an error occrus
132 * a status is shown in the status bar.
133 */
134 public static void gotoLabel() {
135 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), "gotoLabelDefinition()");
136
137 View view = jEdit.getActiveView();
138
139 String word = getCurrentWord(view);
140 if (word == null || word.length() == 0) {
141 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), " no word found");
142 return;
143 }
144 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), " search for " + word);
145 ResourceManager resource = getResource(view);
146 if (resource == null) {
147 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), " resource null ");
148 view.getStatus().setMessage("Parse buffer first");
149 return;
150 }
151 Label label = resource.searchLabel(word);
152
153 if (label == null) {
154 view.getStatus().setMessage(word + " not found");
155 Log.log(Log.DEBUG, jEdit.getPlugin("Plb"), " nothing found");
156 return;
157 }
158 openBuffer(view,label.getSource(),label.getLineno()-1);
159 }
160
161 /***
162 * helper to get the ResourceManager of the actual buffer in the given view.
163 * The resource-manager can be accessed from the PlbSource, which is stored in
164 * PlbParsedData, a subclass of SideKickParsedData, which can be received from
165 * the SideKickPlugin.<br/>
166 * The PlbParsedData can only be received if the buffer was parsed by the the SideKickPlugin
167 * before, so if buffer was not parsed, parsing is performed first.
168 * @param view to handle
169 * @return resource-manager belonging to the view actual buffer.
170 */
171 private static ResourceManager getResource(View view) {
172 Buffer buffer = view.getBuffer();
173 if (!buffer.getMode().getName().equals("plb")) {
174 view.getStatus().setMessage("no plb buffer");
175 Log.log(
176 Log.DEBUG,
177 jEdit.getPlugin("Plb"),
178 " not in plb mode: " + buffer.getMode().getName());
179 return null;
180 }
181 SideKickParsedData sdata = SideKickParsedData.getParsedData(view);
182
183 if (sdata == null || !(sdata instanceof PlbParsedData)) {
184 sidekick.SideKickPlugin.parse(view, true);
185 sdata = SideKickParsedData.getParsedData(view);
186 if (sdata == null || !(sdata instanceof PlbParsedData)) {
187 view.getStatus().setMessage("Buffer not parsed. Parse first.");
188 Log.log(
189 Log.DEBUG,
190 jEdit.getPlugin("Plb"),
191 " no parsed data?!");
192 return null;
193 }
194 }
195
196 PlbParsedData data = (PlbParsedData) sdata;
197 ResourceManager resource;
198 try {
199 resource =
200 data.getSource().getResources(
201 jEdit.getProperty("rsi.plb.path"));
202 } catch (FileNotFoundException e) {
203 Log.log(
204 Log.ERROR,
205 jEdit.getPlugin("Plb"),
206 " FileNotFoundException:" + e.getMessage());
207 return null;
208 } catch (IOException e) {
209 Log.log(
210 Log.ERROR,
211 jEdit.getPlugin("Plb"),
212 " IOException+" + e.getMessage());
213 return null;
214 }
215 return resource;
216 }
217
218 private static void openBuffer(final View view, String path, final int lineNo) {
219 final Buffer buffer;
220 if (jEdit.getBuffer(path) != null) {
221 buffer = jEdit.getBuffer(path);
222 } else {
223 buffer = jEdit.openFile(view, path);
224 if (buffer == null)
225 return;
226 }
227
228 VFSManager.runInAWTThread(new Runnable() {
229 public void run() {
230 // new in 4.2 view.goToBuffer(buffer);
231 view.setBuffer(buffer);
232 view.getTextArea().moveCaretPosition(buffer.getLineStartOffset(lineNo));
233 }
234 });
235 }
236 }
This page was automatically generated by Maven