diff options
author | Adrian Dudau <adrian.dudau@enea.com> | 2013-12-12 13:36:50 +0100 |
---|---|---|
committer | Adrian Dudau <adrian.dudau@enea.com> | 2013-12-12 15:25:03 +0100 |
commit | 41ac47d732eed8392d60d0f6773e5a279d49b999 (patch) | |
tree | cf19d099db9cfdb8d73aa21c31e7aa1cc86ff860 /plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java | |
download | eclipse-poky-juno-41ac47d732eed8392d60d0f6773e5a279d49b999.tar.gz |
Migrated from the internal git server on the dora-enea branch
Signed-off-by: Adrian Dudau <adrian.dudau@enea.com>
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.java | 118 |
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 | */ | ||
4 | package org.yocto.bc.ui.editors.bitbake; | ||
5 | |||
6 | import java.io.File; | ||
7 | import java.util.Map; | ||
8 | |||
9 | import org.eclipse.core.runtime.IProgressMonitor; | ||
10 | import org.eclipse.core.runtime.IStatus; | ||
11 | import org.eclipse.core.runtime.Status; | ||
12 | import org.eclipse.core.runtime.jobs.Job; | ||
13 | import org.eclipse.jface.text.IRegion; | ||
14 | import org.eclipse.jface.text.ITextHover; | ||
15 | import org.eclipse.jface.text.ITextViewer; | ||
16 | import org.eclipse.jface.text.Region; | ||
17 | |||
18 | import org.yocto.bc.bitbake.BBRecipe; | ||
19 | import org.yocto.bc.bitbake.BBSession; | ||
20 | import org.yocto.bc.ui.Activator; | ||
21 | |||
22 | /** | ||
23 | * Maps BB Variables in the editor to BBSession | ||
24 | * @author kgilmer | ||
25 | * | ||
26 | */ | ||
27 | class 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 | ||