diff options
author | Anton Tanasenko | 2015-03-25 13:36:09 +0000 |
---|---|---|
committer | Anton Tanasenko | 2015-03-25 23:42:40 +0000 |
commit | 14f0b211f1faf67ce9ce74a26e2edb26874d1834 (patch) | |
tree | 1d55467f22caf72a816399798a37769bab86ae07 /org.eclipse.m2e.editor.xml | |
parent | 3dbf995bc75f499610564e3c1d614687f62c8d86 (diff) | |
download | m2e-core-14f0b211f1faf67ce9ce74a26e2edb26874d1834.tar.gz m2e-core-14f0b211f1faf67ce9ce74a26e2edb26874d1834.tar.xz m2e-core-14f0b211f1faf67ce9ce74a26e2edb26874d1834.zip |
463014 plugin configuration autocompletion fails with leading <
Fail gracefully with content-assist mojo classloading errors
Change-Id: I75d7aad08c9c1b3cf76745f92644f2f081aae069
Signed-off-by: Anton Tanasenko <atg.sleepless@gmail.com>
Diffstat (limited to 'org.eclipse.m2e.editor.xml')
2 files changed, 78 insertions, 54 deletions
diff --git a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/PomContentAssistProcessor.java b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/PomContentAssistProcessor.java index 9957ab03..8c7faea9 100644 --- a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/PomContentAssistProcessor.java +++ b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/PomContentAssistProcessor.java @@ -50,7 +50,6 @@ import org.eclipse.ui.ISharedImages; import org.eclipse.ui.PlatformUI; import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; import org.eclipse.wst.sse.core.utils.StringUtils; -import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext; import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest; import org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor; @@ -80,7 +79,7 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { //broken protected void addTagNameProposals(ContentAssistRequest contentAssistRequest, int childPosition) { - Node node = getCurrentNode(contentAssistRequest); + Node node = contentAssistRequest.getParent(); String currentNodeName = node.getNodeName(); PomTemplateContext context = PomTemplateContext.fromNodeName(currentNodeName); @@ -99,9 +98,7 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { // when you type <prefix // the downside is that additional typing hides the proposals popup // there has to be a better way though. the xml element completions seems to be coping with it fine.. - contentAssistRequest.setReplacementBeginPosition(contentAssistRequest.getReplacementBeginPosition() - 1); - contentAssistRequest.setReplacementLength(contentAssistRequest.getReplacementLength() + 1); - addProposals(contentAssistRequest, context, getCurrentNode(contentAssistRequest), + addProposals(contentAssistRequest, context, contentAssistRequest.getParent(), contentAssistRequest.getNode(), contentAssistRequest.getMatchString()); } super.addTagNameProposals(contentAssistRequest, childPosition); @@ -109,7 +106,7 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { @Override protected void addTagInsertionProposals(ContentAssistRequest contentAssistRequest, int childPosition) { - Node node = getCurrentNode(contentAssistRequest); + Node node = contentAssistRequest.getParent(); String currentNodeName = node.getNodeName(); PomTemplateContext context = PomTemplateContext.fromNodeName(currentNodeName); @@ -127,18 +124,6 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { super.addTagInsertionProposals(contentAssistRequest, childPosition); } - private Node getCurrentNode(ContentAssistRequest contentAssistRequest) { - Node currentNode = contentAssistRequest.getNode(); - if(DOMRegionContext.XML_TAG_OPEN.equals(contentAssistRequest.getRegion().getType())) { - // when calling content assist just before an opening node, this node is passed in request, - // but we need its container - currentNode = currentNode.getParentNode(); - } else if(currentNode instanceof Text) { - currentNode = currentNode.getParentNode(); - } - return currentNode; - } - private void addProposals(ContentAssistRequest request, PomTemplateContext context) { ITextSelection selection = (ITextSelection) sourceViewer.getSelectionProvider().getSelection(); int offset = request.getReplacementBeginPosition(); @@ -147,13 +132,14 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { offset = selection.getOffset() + selection.getLength(); } + Node parentNode = request.getParent(); String prefix = extractPrefix(sourceViewer, offset); - addExpressionProposal(request, context, getCurrentNode(request), prefix); + addExpressionProposal(request, context, parentNode, prefix); - addGenerateProposals(request, context, getCurrentNode(request), prefix); + addGenerateProposals(request, context, parentNode, prefix); - addProposals(request, context, getCurrentNode(request), prefix); + addProposals(request, context, parentNode, request.getNode(), prefix); } /** @@ -374,13 +360,14 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { return null; } - private void addProposals(ContentAssistRequest request, PomTemplateContext context, Node currentNode, String prefix) { + private void addProposals(ContentAssistRequest request, PomTemplateContext context, Node parentNode, Node node, + String prefix) { if(request != null) { MavenProject prj = XmlUtils.extractMavenProject(sourceViewer); IProject eclipseprj = XmlUtils.extractProject(sourceViewer); ICompletionProposal[] templateProposals = getTemplateProposals(prj, eclipseprj, sourceViewer, - request.getReplacementBeginPosition(), context.getContextTypeId(), currentNode, prefix); + request.getReplacementBeginPosition(), context.getContextTypeId(), parentNode, node, prefix); for(ICompletionProposal proposal : templateProposals) { if(request.shouldSeparate()) { request.addMacro(proposal); @@ -392,7 +379,7 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { } private ICompletionProposal[] getTemplateProposals(MavenProject project, IProject eclipseprj, ITextViewer viewer, - int offset, String contextTypeId, Node currentNode, String prefix) { + int offset, String contextTypeId, Node parentNode, Node node, String prefix) { ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection(); // adjust offset to end of normalized selection @@ -401,7 +388,21 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { } // String prefix = extractPrefix(viewer, offset); - Region region = new Region(offset - prefix.length(), prefix.length()); + + boolean textPrefix = node instanceof Text || parentNode == node; + + try { + if(textPrefix && offset > 0 && viewer.getDocument().getChar(offset - 1) == '<') { + textPrefix = false; + } + } catch(BadLocationException ex) { + } + + if(textPrefix) { + offset -= prefix.length(); + } + + Region region = new Region(offset, prefix.length()); TemplateContext context = createContext(viewer, region, contextTypeId); if(context == null) { return new ICompletionProposal[0]; @@ -434,8 +435,12 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { image = null; } - Template[] templates = templateContext.getTemplates(project, eclipseprj, currentNode, prefix); + Template[] templates = templateContext.getTemplates(project, eclipseprj, parentNode, prefix); for(Template template : templates) { + if(!textPrefix && template.getPattern().startsWith("<")) { + template.setPattern(template.getPattern().substring(1)); + } + TemplateProposal proposal = createProposalForTemplate(prefix, region, context, image, template, false); if(proposal != null) { matches.add(proposal); @@ -463,17 +468,16 @@ public class PomContentAssistProcessor extends XMLContentAssistProcessor { return StringUtils.convertToHTMLContent(super.getAdditionalProposalInfo()); } }; - } else { - return new TemplateProposal(template, context, region, image, getRelevance(template, prefix)) { - public String getAdditionalProposalInfo() { - return getTemplate().getDescription(); - } - - public String getDisplayString() { - return template.getName(); - } - }; } + return new TemplateProposal(template, context, region, image, getRelevance(template, prefix)) { + public String getAdditionalProposalInfo() { + return getTemplate().getDescription(); + } + + public String getDisplayString() { + return template.getName(); + } + }; } } catch(TemplateException e) { // ignore diff --git a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/mojo/MojoParameterMetadataProvider.java b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/mojo/MojoParameterMetadataProvider.java index 82112a7f..07f6b4a4 100644 --- a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/mojo/MojoParameterMetadataProvider.java +++ b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/mojo/MojoParameterMetadataProvider.java @@ -492,19 +492,29 @@ public class MojoParameterMetadataProvider { private static Map<String, Type> getClassProperties(Class<?> clazz) { Map<String, Type> props = new HashMap<>(); - for(Method m : clazz.getMethods()) { - if((m.getModifiers() & Modifier.STATIC) != 0) { - continue; - } + Method[] methods; + try { + methods = clazz.getMethods(); + } catch(Throwable e) { + log.debug("Cannot get methods of " + clazz.getName(), e); + methods = null; + } + + if(methods != null) { + for(Method m : methods) { + if((m.getModifiers() & Modifier.STATIC) != 0) { + continue; + } - String name = m.getName(); + String name = m.getName(); - if((name.startsWith("add") || name.startsWith("set")) && m.getParameterTypes().length == 1) { //$NON-NLS-1$ //$NON-NLS-2$ - String prop = name.substring(3); - if(!prop.isEmpty()) { - prop = Character.toLowerCase(prop.charAt(0)) + prop.substring(1); - if(!props.containsKey(prop)) { - props.put(prop, m.getGenericParameterTypes()[0]); + if((name.startsWith("add") || name.startsWith("set")) && m.getParameterTypes().length == 1) { //$NON-NLS-1$ //$NON-NLS-2$ + String prop = name.substring(3); + if(!prop.isEmpty()) { + prop = Character.toLowerCase(prop.charAt(0)) + prop.substring(1); + if(!props.containsKey(prop)) { + props.put(prop, m.getGenericParameterTypes()[0]); + } } } } @@ -513,17 +523,27 @@ public class MojoParameterMetadataProvider { Class<?> pClazz = clazz; while(pClazz != null && !pClazz.equals(Object.class)) { - for(Field f : pClazz.getDeclaredFields()) { - if((f.getModifiers() & (Modifier.STATIC | Modifier.FINAL)) != 0) { - continue; - } + Field[] fields; + try { + fields = pClazz.getDeclaredFields(); + } catch(Throwable e) { + log.debug("Cannot get declared fields of " + pClazz.getName(), e); + fields = null; + } - String prop = f.getName(); + if(fields != null) { + for(Field f : fields) { + if((f.getModifiers() & (Modifier.STATIC | Modifier.FINAL)) != 0) { + continue; + } + + String prop = f.getName(); - if(!props.containsKey(prop)) { + if(!props.containsKey(prop)) { - props.put(prop, f.getGenericType()); + props.put(prop, f.getGenericType()); + } } } pClazz = pClazz.getSuperclass(); |