Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 115927 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jst/jsp/ui/internal/contentassist/JSPContentAssistProcessor.java (-4 / +15 lines)
Lines 1145-1151 Link Here
1145
			}
1145
			}
1146
		}
1146
		}
1147
		
1147
		
1148
		addTemplates(request, TemplateContextTypeIdsJSP.ALL);
1148
		// bug115927 use original document position for all/any region templates
1149
		addTemplates(request, TemplateContextTypeIdsJSP.ALL, documentPosition);
1149
		return request;
1150
		return request;
1150
	}
1151
	}
1151
1152
Lines 1163-1168 Link Here
1163
	 * @param context
1164
	 * @param context
1164
	 */
1165
	 */
1165
	private void addTemplates(ContentAssistRequest contentAssistRequest, String context) {
1166
	private void addTemplates(ContentAssistRequest contentAssistRequest, String context) {
1167
		addTemplates(contentAssistRequest, context, contentAssistRequest.getReplacementBeginPosition());
1168
	}
1169
	
1170
	/**
1171
	 * Adds templates to the list of proposals
1172
	 * 
1173
	 * @param contentAssistRequest
1174
	 * @param context
1175
	 * @param startOffset
1176
	 */
1177
	private void addTemplates(ContentAssistRequest contentAssistRequest, String context, int startOffset) {
1166
		if (contentAssistRequest == null)
1178
		if (contentAssistRequest == null)
1167
			return;
1179
			return;
1168
		
1180
		
Lines 1174-1180 Link Here
1174
1186
1175
			if (getTemplateCompletionProcessor() != null) {
1187
			if (getTemplateCompletionProcessor() != null) {
1176
				getTemplateCompletionProcessor().setContextType(context);
1188
				getTemplateCompletionProcessor().setContextType(context);
1177
				ICompletionProposal[] proposals = getTemplateCompletionProcessor().computeCompletionProposals(fTextViewer, contentAssistRequest.getReplacementBeginPosition());
1189
				ICompletionProposal[] proposals = getTemplateCompletionProcessor().computeCompletionProposals(fTextViewer, startOffset);
1178
				for (int i = 0; i < proposals.length; ++i) {
1190
				for (int i = 0; i < proposals.length; ++i) {
1179
					if (useProposalList)
1191
					if (useProposalList)
1180
						contentAssistRequest.addProposal(proposals[i]);
1192
						contentAssistRequest.addProposal(proposals[i]);
Lines 1184-1191 Link Here
1184
			}
1196
			}
1185
		}
1197
		}
1186
	}
1198
	}
1187
1199
	
1188
1189
	protected void addEntityProposals(ContentAssistRequest contentAssistRequest, int documentPosition, ITextRegion completionRegion, IDOMNode treeNode) {
1200
	protected void addEntityProposals(ContentAssistRequest contentAssistRequest, int documentPosition, ITextRegion completionRegion, IDOMNode treeNode) {
1190
		// ignore
1201
		// ignore
1191
	}
1202
	}
(-)src/org/eclipse/jst/jsp/ui/internal/contentassist/JSPTemplateCompletionProcessor.java (-3 / +91 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
2
 * Copyright (c) 2004, 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 10-23 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jst.jsp.ui.internal.contentassist;
11
package org.eclipse.jst.jsp.ui.internal.contentassist;
12
12
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.Comparator;
16
import java.util.List;
17
18
import org.eclipse.jface.text.IDocument;
13
import org.eclipse.jface.text.IRegion;
19
import org.eclipse.jface.text.IRegion;
20
import org.eclipse.jface.text.ITextSelection;
14
import org.eclipse.jface.text.ITextViewer;
21
import org.eclipse.jface.text.ITextViewer;
22
import org.eclipse.jface.text.Region;
15
import org.eclipse.jface.text.contentassist.ICompletionProposal;
23
import org.eclipse.jface.text.contentassist.ICompletionProposal;
16
import org.eclipse.jface.text.templates.ContextTypeRegistry;
24
import org.eclipse.jface.text.templates.ContextTypeRegistry;
17
import org.eclipse.jface.text.templates.Template;
25
import org.eclipse.jface.text.templates.Template;
18
import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
26
import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
19
import org.eclipse.jface.text.templates.TemplateContext;
27
import org.eclipse.jface.text.templates.TemplateContext;
20
import org.eclipse.jface.text.templates.TemplateContextType;
28
import org.eclipse.jface.text.templates.TemplateContextType;
29
import org.eclipse.jface.text.templates.TemplateException;
30
import org.eclipse.jface.text.templates.TemplateProposal;
21
import org.eclipse.jface.text.templates.persistence.TemplateStore;
31
import org.eclipse.jface.text.templates.persistence.TemplateStore;
22
import org.eclipse.jst.jsp.ui.internal.JSPUIPlugin;
32
import org.eclipse.jst.jsp.ui.internal.JSPUIPlugin;
23
import org.eclipse.jst.jsp.ui.internal.editor.JSPEditorPluginImageHelper;
33
import org.eclipse.jst.jsp.ui.internal.editor.JSPEditorPluginImageHelper;
Lines 30-41 Link Here
30
 * JSPTemplateCompletionProcessor is asked for content assist proposals, the
40
 * JSPTemplateCompletionProcessor is asked for content assist proposals, the
31
 * jsp content assist processor has already set the context type for
41
 * jsp content assist processor has already set the context type for
32
 * templates.
42
 * templates.
33
 * 
34
 * @plannedfor 1.0
35
 */
43
 */
36
class JSPTemplateCompletionProcessor extends TemplateCompletionProcessor {
44
class JSPTemplateCompletionProcessor extends TemplateCompletionProcessor {
45
	private static final class ProposalComparator implements Comparator {
46
		public int compare(Object o1, Object o2) {
47
			return ((TemplateProposal) o2).getRelevance() - ((TemplateProposal) o1).getRelevance();
48
		}
49
	}
50
51
	private static final Comparator fgProposalComparator = new ProposalComparator();
37
	private String fContextTypeId = null;
52
	private String fContextTypeId = null;
38
53
54
	/*
55
	 * Copied from super class except instead of calling createContext(viewer,
56
	 * region) call createContext(viewer, region, offset) instead
57
	 */
58
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
59
60
		ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
61
62
		// adjust offset to end of normalized selection
63
		if (selection.getOffset() == offset)
64
			offset = selection.getOffset() + selection.getLength();
65
66
		String prefix = extractPrefix(viewer, offset);
67
		Region region = new Region(offset - prefix.length(), prefix.length());
68
		TemplateContext context = createContext(viewer, region, offset);
69
		if (context == null)
70
			return new ICompletionProposal[0];
71
72
		context.setVariable("selection", selection.getText()); // name of the
73
																// selection
74
																// variables
75
																// {line,
76
																// word}_selection
77
																// //$NON-NLS-1$
78
79
		Template[] templates = getTemplates(context.getContextType().getId());
80
81
		List matches = new ArrayList();
82
		for (int i = 0; i < templates.length; i++) {
83
			Template template = templates[i];
84
			try {
85
				context.getContextType().validate(template.getPattern());
86
			}
87
			catch (TemplateException e) {
88
				continue;
89
			}
90
			if (template.matches(prefix, context.getContextType().getId()))
91
				matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
92
		}
93
94
		Collections.sort(matches, fgProposalComparator);
95
96
		return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
97
	}
98
99
	/**
100
	 * Creates a concrete template context for the given region in the
101
	 * document. This involves finding out which context type is valid at the
102
	 * given location, and then creating a context of this type. The default
103
	 * implementation returns a <code>SmartReplaceTemplateContext</code> for
104
	 * the context type at the given location. This takes the offset at which
105
	 * content assist was invoked into consideration.
106
	 * 
107
	 * @param viewer
108
	 *            the viewer for which the context is created
109
	 * @param region
110
	 *            the region into <code>document</code> for which the
111
	 *            context is created
112
	 * @param offset
113
	 *            the original offset where content assist was invoked
114
	 * @return a template context that can handle template insertion at the
115
	 *         given location, or <code>null</code>
116
	 */
117
	private TemplateContext createContext(ITextViewer viewer, IRegion region, int offset) {
118
		// pretty much same code as super.createContext except create SmartReplaceTemplateContext
119
		TemplateContextType contextType = getContextType(viewer, region);
120
		if (contextType != null) {
121
			IDocument document = viewer.getDocument();
122
			return new ReplaceNameTemplateContext(contextType, document, region.getOffset(), region.getLength(), offset);
123
		}
124
		return null;
125
	}
126
	
39
	protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region, int relevance) {
127
	protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region, int relevance) {
40
		return new CustomTemplateProposal(template, context, region, getImage(template), relevance);
128
		return new CustomTemplateProposal(template, context, region, getImage(template), relevance);
41
	}
129
	}
(-)src/org/eclipse/jst/jsp/ui/internal/contentassist/ReplaceNameTemplateContext.java (+109 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jst.jsp.ui.internal.contentassist;
12
13
import org.eclipse.jface.text.BadLocationException;
14
import org.eclipse.jface.text.IDocument;
15
import org.eclipse.jface.text.Position;
16
import org.eclipse.jface.text.templates.DocumentTemplateContext;
17
import org.eclipse.jface.text.templates.Template;
18
import org.eclipse.jface.text.templates.TemplateBuffer;
19
import org.eclipse.jface.text.templates.TemplateContextType;
20
import org.eclipse.jface.text.templates.TemplateException;
21
22
/**
23
 * Just like DocumentTemplateContext except if an insert offset is passed in,
24
 * during evaluation, the "prefix" before the template will be checked to see
25
 * if it matches the template name. If so, overwrite the template name.
26
 * Otherwise, just insert the template at the insert offset location (by not
27
 * overwriting the prefix text)
28
 */
29
public class ReplaceNameTemplateContext extends DocumentTemplateContext {
30
	private int fInsertOffset = -1;
31
32
	/**
33
	 * Creates a document template context.
34
	 * 
35
	 * @param type
36
	 *            the context type
37
	 * @param document
38
	 *            the document this context applies to
39
	 * @param offset
40
	 *            the offset of the document region
41
	 * @param length
42
	 *            the length of the document region
43
	 */
44
	public ReplaceNameTemplateContext(TemplateContextType type, IDocument document, int offset, int length) {
45
		this(type, document, new Position(offset, length));
46
	}
47
48
	/**
49
	 * Creates a document template context. The supplied <code>Position</code>
50
	 * will be queried to compute the <code>getStart</code> and
51
	 * <code>getEnd</code> methods, which will therefore answer updated
52
	 * position data if it is registered with the document.
53
	 * 
54
	 * @param type
55
	 *            the context type
56
	 * @param document
57
	 *            the document this context applies to
58
	 * @param position
59
	 *            the position describing the area of the document which forms
60
	 *            the template context
61
	 * @since 3.1
62
	 */
63
	public ReplaceNameTemplateContext(TemplateContextType type, IDocument document, Position position) {
64
		super(type, document, position);
65
	}
66
67
	/**
68
	 * Creates a document template context.
69
	 * 
70
	 * @param type
71
	 *            the context type
72
	 * @param document
73
	 *            the document this context applies to
74
	 * @param offset
75
	 *            the offset of the document region
76
	 * @param length
77
	 *            the length of the document region
78
	 * @param insertOffset
79
	 *            the offset of the document region where insert was
80
	 *            originally requested
81
	 */
82
	public ReplaceNameTemplateContext(TemplateContextType type, IDocument document, int offset, int length, int insertOffset) {
83
		this(type, document, new Position(offset, length));
84
		fInsertOffset = insertOffset;
85
	}
86
87
	/*
88
	 * @see org.eclipse.jface.text.templates.TemplateContext#evaluate(org.eclipse.jface.text.templates.Template)
89
	 */
90
	public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
91
		TemplateBuffer buffer = super.evaluate(template);
92
		if (buffer != null) {
93
			if (fInsertOffset > -1 && fInsertOffset > getStart()) {
94
				try {
95
					String prefix = getDocument().get(getStart(), fInsertOffset - getStart());
96
					if (!template.getName().startsWith(prefix)) {
97
						// generate a new buffer that actually contains the
98
						// text that was going to be overwritten
99
						buffer = new TemplateBuffer(prefix + buffer.getString(), buffer.getVariables());
100
					}
101
				}
102
				catch (BadLocationException e) {
103
104
				}
105
			}
106
		}
107
		return buffer;
108
	}
109
}

Return to bug 115927