Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: c4a60367b0b19f74eca561d82cf0cf14c9e0a2e9 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2005, 2009 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsp.ui.internal.contentassist;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal;

/**
 * Implements IJavaCompletionProposal for use with JSPProposalCollector.
 *
 * @plannedfor 1.0
 */
public class JSPCompletionProposal extends CustomCompletionProposal implements IJavaCompletionProposal, ICompletionProposalExtension3, ICompletionProposalExtension5 {

	private static final char[] TRIGGERS = new char[] { '.', '[' };
	/*
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=124483
	 * 
	 * This is a wrapped proposal so we don't need to 
	 * make "slow" calls to the java proposal up front, only when needed
	 * for example, getAdditionalInfo() reads external javadoc, and it makes
	 * no sense
	 */ 
	ICompletionProposal fJavaCompletionProposal = null;
	
	public JSPCompletionProposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo, int relevance, boolean updateReplacementLengthOnValidate) {
		super(replacementString, replacementOffset, replacementLength, cursorPosition, image, displayString, contextInformation, additionalProposalInfo, relevance, updateReplacementLengthOnValidate);
	}
	
	/**
	 * Sets cursor position after applying.
	 */
	public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
		if (trigger != (char) 0 ) {
			setReplacementString(getReplacementString() + trigger);
			setCursorPosition(getCursorPosition() + 1);
		}
		super.apply(viewer, trigger, stateMask, offset);
		//move the caret to the end of the change
		int endOffsetOfChanges = getReplacementString().length() + getReplacementOffset();
		viewer.getTextWidget().setCaretOffset(endOffsetOfChanges);
	}

	final public ICompletionProposal getJavaCompletionProposal() {
		return fJavaCompletionProposal;
	}

	final public void setJavaCompletionProposal(ICompletionProposal javaCompletionProposal) {
		fJavaCompletionProposal = javaCompletionProposal;
	}
	
	public String getAdditionalProposalInfo() {
		String additionalInfo = super.getAdditionalProposalInfo();
		ICompletionProposal javaProposal = getJavaCompletionProposal();
		if(javaProposal != null)
			additionalInfo = javaProposal.getAdditionalProposalInfo();
		
		return additionalInfo;
	}

	/**
	 * use the java proposals image if there is one for this proposals image
	 */                                                                     
	public Image getImage() {                                               
		if(this.fJavaCompletionProposal != null) {                          
			return this.fJavaCompletionProposal.getImage();                 
		} else {                                                            
			return super.getImage();                                        
		}                                                                   
	}                                                                       

	/* 
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension5#getAdditionalProposalInfo(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
		Object additionalInfo = super.getAdditionalProposalInfo();
		ICompletionProposal javaProposal = getJavaCompletionProposal();
		if (javaProposal != null) {
			if (javaProposal instanceof ICompletionProposalExtension5)
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=260951
				additionalInfo = ((ICompletionProposalExtension5) javaProposal).getAdditionalProposalInfo(monitor);
			else
				additionalInfo = javaProposal.getAdditionalProposalInfo();
		}

		return additionalInfo;
	}

	/*
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getInformationControlCreator()
	 */
	public IInformationControlCreator getInformationControlCreator() {
		// [277530] Use the java proposal's information control creator
		ICompletionProposal javaProposal = getJavaCompletionProposal();
		IInformationControlCreator informationControlCreator = null;

		if (javaProposal instanceof ICompletionProposalExtension3)
			informationControlCreator = ((ICompletionProposalExtension3) javaProposal).getInformationControlCreator();

		return informationControlCreator;
	}

	/*
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getPrefixCompletionStart(org.eclipse.jface.text.IDocument, int)
	 */
	public int getPrefixCompletionStart(IDocument document, int completionOffset) {
		ICompletionProposal javaProposal = getJavaCompletionProposal();
		if (javaProposal instanceof ICompletionProposalExtension3)
			return ((ICompletionProposalExtension3) javaProposal).getPrefixCompletionStart(document, completionOffset);

		return getReplacementOffset();
	}

	public char[] getTriggerCharacters() {
		return TRIGGERS;
	}

	/*
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getPrefixCompletionText(org.eclipse.jface.text.IDocument, int)
	 */
	public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) {
		ICompletionProposal javaProposal = getJavaCompletionProposal();
		if (javaProposal instanceof ICompletionProposalExtension3)
			return ((ICompletionProposalExtension3) javaProposal).getPrefixCompletionText(document, completionOffset);

		return getReplacementString();
	}
}

Back to the top