summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BitBakeSourceViewerConfiguration.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BitBakeSourceViewerConfiguration.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BitBakeSourceViewerConfiguration.java195
1 files changed, 195 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BitBakeSourceViewerConfiguration.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BitBakeSourceViewerConfiguration.java
new file mode 100644
index 0000000..56cd014
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BitBakeSourceViewerConfiguration.java
@@ -0,0 +1,195 @@
1/*****************************************************************************
2 * Copyright (c) 2009 Ken Gilmer
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Ken Gilmer - initial API and implementation
10 *******************************************************************************/
11package org.yocto.bc.ui.editors.bitbake;
12
13import org.eclipse.core.resources.IFile;
14import org.eclipse.jface.preference.IPreferenceStore;
15import org.eclipse.jface.text.ITextHover;
16import org.eclipse.jface.text.TextAttribute;
17import org.eclipse.jface.text.contentassist.ContentAssistant;
18import org.eclipse.jface.text.contentassist.IContentAssistant;
19import org.eclipse.jface.text.presentation.IPresentationReconciler;
20import org.eclipse.jface.text.presentation.PresentationReconciler;
21import org.eclipse.jface.text.reconciler.IReconciler;
22import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
23import org.eclipse.jface.text.rules.IRule;
24import org.eclipse.jface.text.rules.IToken;
25import org.eclipse.jface.text.rules.IWordDetector;
26import org.eclipse.jface.text.rules.RuleBasedScanner;
27import org.eclipse.jface.text.rules.SingleLineRule;
28import org.eclipse.jface.text.rules.Token;
29import org.eclipse.jface.text.rules.WordRule;
30import org.eclipse.jface.text.source.ISharedTextColors;
31import org.eclipse.jface.text.source.ISourceViewer;
32import org.eclipse.swt.SWT;
33import org.eclipse.swt.graphics.Color;
34import org.eclipse.swt.graphics.RGB;
35import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
36
37import org.yocto.bc.bitbake.BBLanguageHelper;
38import org.yocto.bc.bitbake.BBSession;
39
40public class BitBakeSourceViewerConfiguration extends TextSourceViewerConfiguration {
41
42 private static final class WordDetector implements IWordDetector {
43 public boolean isWordPart(char c) {
44 return !Character.isWhitespace(c);
45 }
46
47 public boolean isWordStart(char c) {
48 return !Character.isWhitespace(c);
49 }
50 }
51
52 private final ISharedTextColors fSharedColors;
53 private BBSession session;
54 private IFile targetFile;
55 private BBVariableTextHover textHover = null;
56
57 public BitBakeSourceViewerConfiguration(ISharedTextColors sharedColors, IPreferenceStore store) {
58 super(store);
59 fSharedColors = sharedColors;
60 }
61
62 protected void setTargetFile(IFile targetFile) {
63 this.targetFile = targetFile;
64 }
65
66 public ITextHover getTextHover(ISourceViewer sv, String contentType) {
67 //only .bb file support Text Hover.
68 if (textHover == null && targetFile.getFileExtension().equals(BBLanguageHelper.BITBAKE_RECIPE_FILE_EXTENSION)) {
69 textHover = new BBVariableTextHover(session, targetFile.getLocationURI().getPath());
70 }
71
72 return textHover;
73 }
74
75 private void addDamagerRepairer(PresentationReconciler reconciler, RuleBasedScanner commentScanner, String contentType) {
76 DefaultDamagerRepairer commentDamagerRepairer = new DefaultDamagerRepairer(commentScanner);
77 reconciler.setDamager(commentDamagerRepairer, contentType);
78 reconciler.setRepairer(commentDamagerRepairer, contentType);
79 }
80
81 private RuleBasedScanner createCommentScanner() {
82 Color green = fSharedColors.getColor(new RGB(16, 96, 16));
83 RuleBasedScanner commentScanner = new RuleBasedScanner();
84 commentScanner.setDefaultReturnToken(new Token(new TextAttribute(green, null, SWT.ITALIC)));
85 return commentScanner;
86 }
87
88 private IRule createCustomFunctionRule() {
89 Color blue = fSharedColors.getColor(new RGB(130, 0, 0));
90 IRule rule = new CustomFunctionRule(new Token(new TextAttribute(blue, null, SWT.BOLD)));
91
92 return rule;
93 }
94
95 private SingleLineRule createFunctionNameRule() {
96 Color red = fSharedColors.getColor(new RGB(150, 0, 96));
97 SingleLineRule stepRule = new SingleLineRule("do_", ")", new Token(new TextAttribute(red, null, SWT.BOLD))); //$NON-NLS-1$ //$NON-NLS-2$
98 stepRule.setColumnConstraint(0);
99 return stepRule;
100 }
101
102 private SingleLineRule createInlineVariableRule() {
103 Color blue = fSharedColors.getColor(new RGB(50, 50, 100));
104 SingleLineRule stepRule = new SingleLineRule("${", "}", new Token(new TextAttribute(blue, null, SWT.BOLD))); //$NON-NLS-1$ //$NON-NLS-2$
105 return stepRule;
106 }
107
108 private WordRule createKeywordRule() {
109 WordRule keywordRule = new WordRule(new WordDetector());
110 IToken token = new Token(new TextAttribute(fSharedColors.getColor(new RGB(96, 96, 0)), null, SWT.NONE));
111
112 for (int i = 0; i < BBLanguageHelper.BITBAKE_KEYWORDS.length; ++i) {
113
114 keywordRule.addWord(BBLanguageHelper.BITBAKE_KEYWORDS[i], token);
115 keywordRule.setColumnConstraint(0);
116 }
117
118 return keywordRule;
119 }
120
121 private RuleBasedScanner createRecipeScanner() {
122 RuleBasedScanner recipeScanner = new RuleBasedScanner();
123
124 IRule[] rules = { createKeywordRule(), createShellKeywordRule(), createStringLiteralRule(), createVariableRule(), createFunctionNameRule(), createCustomFunctionRule(),
125 createInlineVariableRule() };
126 recipeScanner.setRules(rules);
127 return recipeScanner;
128 }
129
130 private WordRule createShellKeywordRule() {
131 WordRule keywordRule = new WordRule(new WordDetector());
132 IToken token = new Token(new TextAttribute(fSharedColors.getColor(new RGB(0, 64, 92)), null, SWT.NONE));
133
134 for (int i = 0; i < BBLanguageHelper.SHELL_KEYWORDS.length; ++i) {
135 keywordRule.addWord(BBLanguageHelper.SHELL_KEYWORDS[i], token);
136 }
137
138 return keywordRule;
139 }
140
141 private SingleLineRule createStringLiteralRule() {
142 Color red = fSharedColors.getColor(new RGB(50, 50, 100));
143 SingleLineRule rule = new SingleLineRule("\"", "\"", new Token(new TextAttribute(red, null, SWT.NONE)), '\\');
144
145 return rule;
146 }
147
148 private IRule createVariableRule() {
149 Color blue = fSharedColors.getColor(new RGB(0, 0, 200));
150 IRule rule = new VariableRule(new Token(new TextAttribute(blue, null, SWT.NONE)));
151
152 return rule;
153 }
154
155 @Override
156 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
157 return new String[] { BitBakeDocumentProvider.RECIPE_CODE, BitBakeDocumentProvider.RECIPE_COMMENT };
158 }
159
160 @Override
161 public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
162 return BitBakeDocumentProvider.RECIPE_PARTITIONING;
163 }
164
165 @Override
166 public IContentAssistant getContentAssistant(final ISourceViewer sourceViewer) {
167 ContentAssistant assistant = new ContentAssistant();
168 assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
169
170 // assistant.setContentAssistProcessor(new HippieProposalProcessor(),
171 // BitBakeDocumentProvider.RECIPE_COMMENT);
172 assistant.setContentAssistProcessor(new RecipeCompletionProcessor(), BitBakeDocumentProvider.RECIPE_CODE);
173
174 return assistant;
175 }
176
177 public IReconciler getReconciler(ISourceViewer sourceViewer) {
178 return null;
179 }
180
181 @Override
182 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
183 PresentationReconciler reconciler = new PresentationReconciler();
184 reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
185
186 addDamagerRepairer(reconciler, createCommentScanner(), BitBakeDocumentProvider.RECIPE_COMMENT);
187 addDamagerRepairer(reconciler, createRecipeScanner(), BitBakeDocumentProvider.RECIPE_CODE);
188
189 return reconciler;
190 }
191
192 public void setBBSession(BBSession session) {
193 this.session = session;
194 }
195}