Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ade04e42cc39b65a3b221dda321944d38bf5666e (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
121
122
123
124
125
126
127
128
129
/*******************************************************************************
 * Copyright (c) 2008, 2012 Symbian 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
 *
 * Contributors:
 * Andrew Ferguson (Symbian) - Initial implementation
 *******************************************************************************/
package org.eclipse.cdt.ui.text.doctools.generic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProposal;
import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.ui.text.contentassist.ContentAssistInvocationContext;
import org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;

/**
 * CompletionProposalComputer based on a specified set of GenericTag objects.
 * @since 5.0
 * @noextend This class is not intended to be subclassed by clients.
 */
public class GenericTagCompletionProposalComputer implements ICompletionProposalComputer {
	protected GenericDocTag[] tags;
	protected char[] tagMarkers;

	/**
	 * Constructs a proposal computer for the specified tags
	 * @param tags
	 */
	public GenericTagCompletionProposalComputer(GenericDocTag[] tags, char[] tagMarkers) {
		this.tags = tags;
		this.tagMarkers = tagMarkers;
	}

	/**
	 * @param c the character to test
	 * @return whether the specified character is a tag prefix marker
	 */
	protected boolean isTagMarker(char c) {
		for (char candidate : tagMarkers)
			if (c == candidate)
				return true;
		return false;
	}

	/*
	 * @see org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.cdt.ui.text.contentassist.ContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context,
			IProgressMonitor monitor) {
		IDocument doc = context.getDocument();
		int ivcOffset = context.getInvocationOffset();
		try {
			ITypedRegion tr = TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING, ivcOffset, false);
			int firstNonWS = ivcOffset;
			while (firstNonWS - 1 > tr.getOffset() && !Character.isWhitespace(doc.get(firstNonWS - 1, 1).charAt(0)))
				firstNonWS--;
			String prefix = doc.get(firstNonWS, ivcOffset - firstNonWS);
			if (prefix.length() > 0 && isTagMarker(prefix.charAt(0))) {
				List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
				char tagMarker = prefix.charAt(0);
				for (GenericDocTag tag2 : tags) {
					String tag = tag2.getTagName();
					if (tag.toLowerCase().startsWith(prefix.substring(1).toLowerCase())) {
						CCompletionProposal proposal = new CCompletionProposal(tagMarker + tag,
								ivcOffset - prefix.length(), prefix.length(), null, tagMarker + tag, 1,
								context.getViewer());
						String description = tag2.getTagDescription();
						if (description != null && description.length() > 0) {
							proposal.setAdditionalProposalInfo(description);
						}
						proposals.add(proposal);
					}
				}
				return proposals;
			}
		} catch (BadLocationException ble) {
			// offset is zero, ignore
		}
		return Collections.emptyList();
	}

	/*
	 * @see org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.cdt.ui.text.contentassist.ContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public List<IContextInformation> computeContextInformation(ContentAssistInvocationContext context,
			IProgressMonitor monitor) {
		return Collections.emptyList();
	}

	/*
	 * @see org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer#getErrorMessage()
	 */
	@Override
	public String getErrorMessage() {
		return null;
	}

	/*
	 * @see org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer#sessionEnded()
	 */
	@Override
	public void sessionEnded() {
	}

	/*
	 * @see org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer#sessionStarted()
	 */
	@Override
	public void sessionStarted() {
	}
}

Back to the top