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