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/wst/html/ui/internal/contentassist/HTMLContentAssistProcessor.java (-4 / +16 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 119-127 Link Here
119
	 * @param context
119
	 * @param context
120
	 */
120
	 */
121
	private void addTemplates(ContentAssistRequest contentAssistRequest, String context) {
121
	private void addTemplates(ContentAssistRequest contentAssistRequest, String context) {
122
		addTemplates(contentAssistRequest, context, contentAssistRequest.getReplacementBeginPosition());
123
	}
124
	
125
	/**
126
	 * Adds templates to the list of proposals
127
	 * 
128
	 * @param contentAssistRequest
129
	 * @param context
130
	 * @param startOffset
131
	 */
132
	private void addTemplates(ContentAssistRequest contentAssistRequest, String context, int startOffset) {
122
		if (contentAssistRequest == null)
133
		if (contentAssistRequest == null)
123
			return;
134
			return;
124
135
		
125
		// if already adding template proposals for a certain context type, do
136
		// if already adding template proposals for a certain context type, do
126
		// not add again
137
		// not add again
127
		if (!fTemplateContexts.contains(context)) {
138
		if (!fTemplateContexts.contains(context)) {
Lines 130-136 Link Here
130
141
131
			if (getTemplateCompletionProcessor() != null) {
142
			if (getTemplateCompletionProcessor() != null) {
132
				getTemplateCompletionProcessor().setContextType(context);
143
				getTemplateCompletionProcessor().setContextType(context);
133
				ICompletionProposal[] proposals = getTemplateCompletionProcessor().computeCompletionProposals(fTextViewer, contentAssistRequest.getReplacementBeginPosition());
144
				ICompletionProposal[] proposals = getTemplateCompletionProcessor().computeCompletionProposals(fTextViewer, startOffset);
134
				for (int i = 0; i < proposals.length; ++i) {
145
				for (int i = 0; i < proposals.length; ++i) {
135
					if (useProposalList)
146
					if (useProposalList)
136
						contentAssistRequest.addProposal(proposals[i]);
147
						contentAssistRequest.addProposal(proposals[i]);
Lines 151-157 Link Here
151
162
152
	protected ContentAssistRequest computeCompletionProposals(int documentPosition, String matchString, ITextRegion completionRegion, IDOMNode treeNode, IDOMNode xmlnode) {
163
	protected ContentAssistRequest computeCompletionProposals(int documentPosition, String matchString, ITextRegion completionRegion, IDOMNode treeNode, IDOMNode xmlnode) {
153
		ContentAssistRequest request = super.computeCompletionProposals(documentPosition, matchString, completionRegion, treeNode, xmlnode);
164
		ContentAssistRequest request = super.computeCompletionProposals(documentPosition, matchString, completionRegion, treeNode, xmlnode);
154
		addTemplates(request, TemplateContextTypeIdsHTML.ALL);
165
		// bug115927 use original document position for all/any region templates
166
		addTemplates(request, TemplateContextTypeIdsHTML.ALL, documentPosition);
155
		return request;
167
		return request;
156
	}
168
	}
157
169
(-)src/org/eclipse/wst/html/ui/internal/contentassist/HTMLTemplateCompletionProcessor.java (-1 / +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.wst.html.ui.internal.contentassist;
11
package org.eclipse.wst.html.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.swt.graphics.Image;
32
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.wst.html.ui.internal.HTMLUIPlugin;
33
import org.eclipse.wst.html.ui.internal.HTMLUIPlugin;
Lines 33-40 Link Here
33
 * templates.
43
 * templates.
34
 */
44
 */
35
class HTMLTemplateCompletionProcessor extends TemplateCompletionProcessor {
45
class HTMLTemplateCompletionProcessor extends TemplateCompletionProcessor {
46
	private static final class ProposalComparator implements Comparator {
47
		public int compare(Object o1, Object o2) {
48
			return ((TemplateProposal) o2).getRelevance() - ((TemplateProposal) o1).getRelevance();
49
		}
50
	}
51
52
	private static final Comparator fgProposalComparator = new ProposalComparator();
36
	private String fContextTypeId = null;
53
	private String fContextTypeId = null;
37
54
55
	/*
56
	 * Copied from super class except instead of calling createContext(viewer,
57
	 * region) call createContext(viewer, region, offset) instead
58
	 */
59
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
60
61
		ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
62
63
		// adjust offset to end of normalized selection
64
		if (selection.getOffset() == offset)
65
			offset = selection.getOffset() + selection.getLength();
66
67
		String prefix = extractPrefix(viewer, offset);
68
		Region region = new Region(offset - prefix.length(), prefix.length());
69
		TemplateContext context = createContext(viewer, region, offset);
70
		if (context == null)
71
			return new ICompletionProposal[0];
72
73
		context.setVariable("selection", selection.getText()); // name of the
74
																// selection
75
																// variables
76
																// {line,
77
																// word}_selection
78
																// //$NON-NLS-1$
79
80
		Template[] templates = getTemplates(context.getContextType().getId());
81
82
		List matches = new ArrayList();
83
		for (int i = 0; i < templates.length; i++) {
84
			Template template = templates[i];
85
			try {
86
				context.getContextType().validate(template.getPattern());
87
			}
88
			catch (TemplateException e) {
89
				continue;
90
			}
91
			if (template.matches(prefix, context.getContextType().getId()))
92
				matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
93
		}
94
95
		Collections.sort(matches, fgProposalComparator);
96
97
		return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
98
	}
99
100
	/**
101
	 * Creates a concrete template context for the given region in the
102
	 * document. This involves finding out which context type is valid at the
103
	 * given location, and then creating a context of this type. The default
104
	 * implementation returns a <code>SmartReplaceTemplateContext</code> for
105
	 * the context type at the given location. This takes the offset at which
106
	 * content assist was invoked into consideration.
107
	 * 
108
	 * @param viewer
109
	 *            the viewer for which the context is created
110
	 * @param region
111
	 *            the region into <code>document</code> for which the
112
	 *            context is created
113
	 * @param offset
114
	 *            the original offset where content assist was invoked
115
	 * @return a template context that can handle template insertion at the
116
	 *         given location, or <code>null</code>
117
	 */
118
	private TemplateContext createContext(ITextViewer viewer, IRegion region, int offset) {
119
		// pretty much same code as super.createContext except create SmartReplaceTemplateContext
120
		TemplateContextType contextType = getContextType(viewer, region);
121
		if (contextType != null) {
122
			IDocument document = viewer.getDocument();
123
			return new ReplaceNameTemplateContext(contextType, document, region.getOffset(), region.getLength(), offset);
124
		}
125
		return null;
126
	}
127
38
	protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region, int relevance) {
128
	protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region, int relevance) {
39
		return new CustomTemplateProposal(template, context, region, getImage(template), relevance);
129
		return new CustomTemplateProposal(template, context, region, getImage(template), relevance);
40
	}
130
	}
(-)src/org/eclipse/wst/html/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.wst.html.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