Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1d7dde76579c51727aa155d2e93b14a3b7d362e1 (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
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
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems 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
 *******************************************************************************/
package org.eclipse.cdt.internal.qt.ui.editor;

import java.io.File;
import java.util.Collection;

import javax.script.ScriptException;

import org.eclipse.cdt.internal.qt.ui.Activator;
import org.eclipse.cdt.qt.core.IQMLAnalyzer;
import org.eclipse.cdt.qt.core.QMLTernCompletion;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
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;
import org.eclipse.ui.IFileEditorInput;

public class QMLContentAssistProcessor implements IContentAssistProcessor {
	private static final IContextInformation[] NO_CONTEXTS = {};
	private static final ICompletionProposal[] NO_COMPLETIONS = {};

	private final IQMLAnalyzer analyzer = Activator.getService(IQMLAnalyzer.class);
	private final QMLEditor editor;

	public QMLContentAssistProcessor(QMLEditor editor) {
		this.editor = editor;
	}

	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		if (analyzer == null || !analyzer.isSupported()) {
			return NO_COMPLETIONS;
		}
		IDocument document = viewer.getDocument();
		String prefix = lastWord(document, offset);
		// Save the file
		IFileEditorInput fileInput = (IFileEditorInput) editor.getEditorInput();
		String fileName = new File(fileInput.getFile().getLocationURI()).getAbsolutePath();

		try {
			String contents = document.get();
			Collection<QMLTernCompletion> completions = analyzer.getCompletions(fileName, contents, offset);
			if (!completions.isEmpty()) {
				ICompletionProposal[] proposals = new ICompletionProposal[completions.size()];
				int i = 0;
				for (QMLTernCompletion completion : completions) {
					String name = completion.getName();
					String type = completion.getType();
					String displayString = name;
					if (type != null) {
						displayString += " : " + completion.getType(); //$NON-NLS-1$
					}
					proposals[i++] = new CompletionProposal(name, offset - prefix.length(), prefix.length(),
							name.length(), null, displayString, null, completion.getOrigin());
				}
				return proposals;
			}
		} catch (NoSuchMethodException | ScriptException e) {
			Activator.log(e);
		}
		return NO_COMPLETIONS;
	}

	private String lastWord(IDocument document, int offset) {
		try {
			for (int n = offset - 1; n >= 0; n--) {
				char c = document.getChar(n);
				if (!Character.isJavaIdentifierPart(c)) {
					return document.get(n + 1, offset - n - 1);
				}
			}
			return document.get(0, offset);
		} catch (BadLocationException e) {
			Activator.log(e);
		}
		return ""; //$NON-NLS-1$
	}

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

	@Override
	public char[] getCompletionProposalAutoActivationCharacters() {
		return new char[] { '.' };
	}

	@Override
	public char[] getContextInformationAutoActivationCharacters() {
		// TODO not sure
		return null;
	}

	@Override
	public String getErrorMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IContextInformationValidator getContextInformationValidator() {
		// TODO Auto-generated method stub
		return null;
	}

}

Back to the top