aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/ui/editors/bitbake/RecipeCompletionProcessor.java
blob: c41012458a4309ab639a6661f804f3fc72ff4230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*****************************************************************************
 * Copyright (c) 2009 Ken Gilmer
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Ken Gilmer - initial API and implementation
 *******************************************************************************/
package org.openembedded.bc.ui.editors.bitbake;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.jface.text.templates.DocumentTemplateContext;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.eclipse.jface.text.templates.TemplateProposal;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.openembedded.bc.bitbake.BBLanguageHelper;
import org.openembedded.bc.ui.Activator;


class RecipeCompletionProcessor implements IContentAssistProcessor {

	private static final String CONTEXT_ID= "bitbake_variables"; //$NON-NLS-1$
	private final TemplateContextType fContextType= new TemplateContextType(CONTEXT_ID, "Common BitBake Variables"); //$NON-NLS-1$
	//private final TemplateContextType fKeywordContextType= new TemplateContextType("bitbake_keywords", "BitBake Keywords"); //$NON-NLS-1$
	private final TemplateContextType fFunctionContextType = new TemplateContextType("bitbake_functions", "BitBake Functions");

	RecipeCompletionProcessor() {
	}

	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		IDocument document= viewer.getDocument();
		Region region= new Region(offset, 0);
		
		TemplateContext templateContext= new DocumentTemplateContext(fContextType, document, offset, 0);
		//TemplateContext keywordContext = new DocumentTemplateContext(fKeywordContextType, document, offset, 0);
		TemplateContext functionContext = new DocumentTemplateContext(fFunctionContextType, document, offset, 0);
		
		List proposals = new ArrayList();
		
		getVariableTemplateProposals(templateContext, region, proposals);
		// getKeywordTemplateProposals(keywordContext, region, proposals);
		getAddTaskTemplateProposals(templateContext, region, proposals);
		getFunctionTemplateProposals(functionContext, region, proposals);
		
		return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
	}
	
	public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
		return null;
	}
	
	private Template generateVariableTemplate(String name, String description) {

		return new Template(name, description, CONTEXT_ID, name + " = \"${" + name.toLowerCase() + "}\"", false);
	}

	private void getAddTaskTemplateProposals(TemplateContext templateContext, Region region, List p) {
			p.add(new TemplateProposal(new Template("addtask", "addtask statement", CONTEXT_ID, "addtask ${task_name} after ${do_previous_task} before ${do_next_task}", false),templateContext, region, PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_BKMRK_TSK)));
	}


	public char[] getCompletionProposalAutoActivationCharacters() {
		return null;
	}

	public char[] getContextInformationAutoActivationCharacters() {
		return null;
	}
	
	public IContextInformationValidator getContextInformationValidator() {
		return null;
	}

	public String getErrorMessage() {
		return null;
	}

	private void getFunctionTemplateProposals(TemplateContext templateContext, Region region, List p) {
		String [] keywords = BBLanguageHelper.BITBAKE_STANDARD_FUNCTIONS;
		Image img = Activator.getDefault().getImageRegistry().get(Activator.IMAGE_FUNCTION);
		Arrays.sort(keywords);
		
		for (int i = 0; i < keywords.length; ++i) {
			p.add(new TemplateProposal(new Template(keywords[i], keywords[i] + " function", CONTEXT_ID, "do_" + keywords[i] + "() {\n\n}", false), templateContext, region, img));
		}
	}

	private void getKeywordTemplateProposals(TemplateContext templateContext, Region region, List p) {
		String [] keywords = BBLanguageHelper.BITBAKE_KEYWORDS;
		
		Arrays.sort(keywords);
		
		for (int i = 0; i < keywords.length; ++i) {
			p.add(new TemplateProposal(new Template(keywords[i], keywords[i] + " keyword", CONTEXT_ID, keywords[i] + " ", false),templateContext, region, null));
		}
	}

	private void getVariableTemplateProposals(TemplateContext templateContext, Region region, List p) {
		Map n = BBLanguageHelper.getCommonBitbakeVariables();
		Image img = Activator.getDefault().getImageRegistry().get(Activator.IMAGE_VARIABLE);
		for (Iterator i = n.keySet().iterator(); i.hasNext();) {
			String name = (String) i.next();
			String description = (String) n.get(name);
			p.add(new TemplateProposal(generateVariableTemplate(name, description), templateContext, region, img));	
		}	
	}
}