summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java118
1 files changed, 118 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java
new file mode 100644
index 0000000..9478423
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java
@@ -0,0 +1,118 @@
1/**
2 *
3 */
4package org.yocto.bc.ui.editors.bitbake;
5
6import java.io.File;
7import java.util.Map;
8
9import org.eclipse.core.runtime.IProgressMonitor;
10import org.eclipse.core.runtime.IStatus;
11import org.eclipse.core.runtime.Status;
12import org.eclipse.core.runtime.jobs.Job;
13import org.eclipse.jface.text.IRegion;
14import org.eclipse.jface.text.ITextHover;
15import org.eclipse.jface.text.ITextViewer;
16import org.eclipse.jface.text.Region;
17
18import org.yocto.bc.bitbake.BBRecipe;
19import org.yocto.bc.bitbake.BBSession;
20import org.yocto.bc.ui.Activator;
21
22/**
23 * Maps BB Variables in the editor to BBSession
24 * @author kgilmer
25 *
26 */
27class BBVariableTextHover implements ITextHover {
28 private final BBSession session;
29 private volatile Map envMap;
30
31 public BBVariableTextHover(BBSession session, String file) {
32 this.session = session;
33 envMap = session;
34 LoadRecipeJob loadRecipeJob = new LoadRecipeJob(getFilename(file), file);
35 loadRecipeJob.schedule();
36 }
37
38 private String getFilename(String file) {
39
40 String [] elems = file.split(File.separator);
41
42 return elems[elems.length - 1];
43 }
44
45 public IRegion getHoverRegion(ITextViewer tv, int off) {
46 return new Region(off, 0);
47 }
48
49 public String getHoverInfo(ITextViewer tv, IRegion r) {
50 try {
51 IRegion lineRegion = tv.getDocument().getLineInformationOfOffset(r.getOffset());
52
53 return getBBVariable(tv.getDocument().get(lineRegion.getOffset(), lineRegion.getLength()).toCharArray(), r.getOffset() - lineRegion.getOffset());
54 } catch (Exception e) {
55 return "";
56 }
57 }
58
59 private String getBBVariable(char[] line, int offset) {
60 // Find start of word.
61 int i = offset;
62
63 while (line[i] != ' ' && line[i] != '$' && i > 0) {
64 i--;
65 }
66
67 if (i < 0 || line[i] != '$') {
68 return ""; //this is not a BB variable.
69 }
70
71 // find end of word
72 int start = i;
73 i = offset;
74
75 while (line[i] != ' ' && line[i] != '}' && i <= line.length) {
76 i++;
77 }
78
79 if (line[i] != '}') {
80 return ""; //this bb variable didn't terminate as expected
81 }
82
83 String key = new String(line, start + 2, i - start - 2);
84 String val = (String) envMap.get(key);
85
86 if (val == null) {
87 val = "";
88 }
89
90 if (val.length() > 64) {
91 val = val.substring(0, 64) + '\n' + val.substring(65);
92 }
93
94 return val;
95 }
96
97 private class LoadRecipeJob extends Job {
98 private final String filePath;
99
100 public LoadRecipeJob(String name, String filePath) {
101 super("Extracting BitBake environment for " + name);
102 this.filePath = filePath;
103 }
104
105 @Override
106 protected IStatus run(IProgressMonitor mon) {
107 try {
108 BBRecipe recipe = Activator.getBBRecipe(session, filePath);
109 recipe.initialize();
110 envMap = recipe;
111 } catch (Exception e) {
112 return new Status(IStatus.WARNING, Activator.PLUGIN_ID, "Unable to load session for " + filePath, e);
113 }
114
115 return Status.OK_STATUS;
116 }
117 }
118} \ No newline at end of file