Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a61f6580fb9755d18ebc7c082b427be97ca5f41e (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2016, 2017 Red Hat Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * - Mickael Istria, Sopot Cela (Red Hat Inc.)
 *******************************************************************************/
package org.eclipse.ui.genericeditor.examples.dotproject;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNatureDescriptor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
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;

public class NaturesAndProjectsContentAssistProcessor implements IContentAssistProcessor {

	public NaturesAndProjectsContentAssistProcessor() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		String text = viewer.getDocument().get();
		String natureTag= "<nature>";
		String projectReferenceTag="<project>";
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		int natureTagLength = natureTag.length();
		if (text.length() >= natureTagLength && offset >= natureTagLength && text.substring(offset - natureTagLength, offset).equals(natureTag)) {
			IProjectNatureDescriptor[] natureDescriptors= workspace.getNatureDescriptors();
			ICompletionProposal[] proposals = new ICompletionProposal[natureDescriptors.length];
			for (int i= 0; i < natureDescriptors.length; i++) {
				IProjectNatureDescriptor descriptor= natureDescriptors[i];
				proposals[i] = new CompletionProposal(descriptor.getNatureId(), offset, 0, descriptor.getNatureId().length());
			}
			return proposals;
		}
		int projectReferenceTagLength = projectReferenceTag.length();
		if (text.length() >= projectReferenceTagLength && offset >= projectReferenceTagLength && text.substring(offset - projectReferenceTagLength, offset).equals(projectReferenceTag)) {
			IProject[] projects= workspace.getRoot().getProjects();
			//TODO - filter out the project this file is in
			ICompletionProposal[] proposals = new ICompletionProposal[projects.length];
			for (int i= 0; i < projects.length; i++) {
				proposals[i]=new CompletionProposal(projects[i].getName(), offset, 0, projects[i].getName().length());
			}
			return proposals;
		}
		return new ICompletionProposal[0];
	}

	@Override
	public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
		return null;
	}

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

	@Override
	public char[] getContextInformationAutoActivationCharacters() {
		return null;
	}

	@Override
	public String getErrorMessage() {
		return null;
	}

	@Override
	public IContextInformationValidator getContextInformationValidator() {
		return null;
	}

}

Back to the top