/** * Creates a template proposal with a template and its context. * * @param template * the template * @param context * the context in which the template was requested. * @param image * the icon of the proposal. * @param region * the region this proposal is applied to * @param relevance * the relevance of the proposal */ public TweakedTemplateProposal(Template template, TemplateContext context, IRegion region, Image image, int relevance) { Assert.isNotNull(template); Assert.isNotNull(context); Assert.isNotNull(region); fTemplate = template; Check.isTrue(context instanceof FtcDocumentTemplateContext); fContext = (FtcDocumentTemplateContext) context; fImage = image; fRegion = region; fDisplayString = null; fRelevance = relevance; }
@Override protected String[] resolveAll(TemplateContext context) { Check.isTrue(context instanceof FtcDocumentTemplateContext); FtcDocumentTemplateContext ftcContext = (FtcDocumentTemplateContext) context; String pattern = ftcContext.getCurrentTemplate().getPattern(); Check.notNull(pattern); String currentText = ftcContext.getDocument().get(); ICompletionProposal[] proposals = createCompletionProposals(ftcContext.getCompletionOffset(), getType(), pattern, currentText); String[] result = new String[proposals.length]; for (int i = 0; i < proposals.length; i++) result[i] = proposals[i].getDisplayString(); return result; }
@Override protected void createTemplates(final TemplateContext templateContext, final ContentAssistContext context, final ITemplateAcceptor acceptor) { if (templateContext.getContextType().getId().equals("com.avaloq.tools.ddk.checkcfg.CheckCfg.ConfiguredCheck")) { //$NON-NLS-1$ addConfiguredCheckTemplates(templateContext, context, acceptor); return; } else if (templateContext.getContextType().getId().equals("com.avaloq.tools.ddk.checkcfg.CheckCfg.kw_catalog")) { //$NON-NLS-1$ addCatalogConfigurations(templateContext, context, acceptor); } TemplateContextType contextType = templateContext.getContextType(); Template[] templates = templateStore.getTemplates(contextType.getId()); for (Template template : templates) { if (!acceptor.canAcceptMoreTemplates()) { return; } if (validate(template, templateContext)) { acceptor.accept(createProposal(template, templateContext, context, getImage(template), getRelevance(template))); } } }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { variable.setUnambiguous(true); variable.setValue(""); // $NON-NLS-1$ if (context instanceof JavaContext) { JavaContext jc = (JavaContext) context; List<String> params = variable.getVariableType().getParams(); if (params.size() > 0) { for (Iterator<String> iterator = params.iterator(); iterator.hasNext(); ) { String typeName = iterator.next(); jc.addImport(typeName); } } } else { super.resolve(variable, context); } }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { variable.setUnambiguous(true); variable.setValue(""); // $NON-NLS-1$ if (context instanceof JavaContext) { JavaContext jc = (JavaContext) context; List<String> params = variable.getVariableType().getParams(); if (params.size() > 0) { for (Iterator<String> iterator = params.iterator(); iterator.hasNext(); ) { String qualifiedMemberName = iterator.next(); jc.addStaticImport(qualifiedMemberName); } } } else { super.resolve(variable, context); } }
@Override protected String resolve(TemplateContext context) { IJavaElement element = ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD); if (element == null) return null; IMethod method = (IMethod) element; try { String[] arguments = method.getParameterNames(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < arguments.length; i++) { if (i > 0) buffer.append(", "); // $NON-NLS-1$ buffer.append(arguments[i]); } return buffer.toString(); } catch (JavaModelException e) { return null; } }
/** * Formats the template buffer. * * @param buffer * @param context * @throws BadLocationException */ public void format(TemplateBuffer buffer, TemplateContext context) throws BadLocationException { try { VariableTracker tracker = new VariableTracker(buffer); IDocument document = tracker.getDocument(); internalFormat(document, context); convertLineDelimiters(document); if (!(context instanceof JavaDocContext) && !isReplacedAreaEmpty(context)) trimStart(document); tracker.updateBuffer(); } catch (MalformedTreeException e) { throw new BadLocationException(); } }
private boolean isReplacedAreaEmpty(TemplateContext context) { // don't trim the buffer if the replacement area is empty // case: surrounding empty lines with block if (context instanceof DocumentTemplateContext) { DocumentTemplateContext dtc = (DocumentTemplateContext) context; if (dtc.getStart() == dtc.getCompletionOffset()) try { IDocument document = dtc.getDocument(); int lineOffset = document.getLineInformationOfOffset(dtc.getStart()).getOffset(); // only if we are at the beginning of the line if (lineOffset != dtc.getStart()) return false; // Does the selection only contain whitespace characters? if (document.get(dtc.getStart(), dtc.getEnd() - dtc.getStart()).trim().length() == 0) return true; } catch (BadLocationException x) { // ignore - this may happen when the document was modified after the initial invocation, // and the // context does not track the changes properly - don't trim in that case return true; } } return false; }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { if (!(variable instanceof MultiVariable)) { super.resolve(variable, context); return; } MultiVariable mv = (MultiVariable) variable; List<String> params = variable.getVariableType().getParams(); if (params.isEmpty()) { super.resolve(variable, context); return; } JavaContext jc = (JavaContext) context; String reference = params.get(0); TemplateVariable refVar = jc.getTemplateVariable(reference); if (refVar instanceof JavaVariable) { JavaVariable jvar = (JavaVariable) refVar; resolve(mv, jvar, jc); return; } super.resolve(variable, context); }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { if (variable instanceof MultiVariable) { JavaContext jc = (JavaContext) context; JavaVariable jv = (JavaVariable) variable; Variable[] iterables = getVariables(jc); if (iterables.length > 0) { jv.setChoices(iterables); jc.markAsUsed(iterables[0].getName()); if (iterables.length > 1) variable.setUnambiguous(false); else variable.setUnambiguous(isUnambiguous(context)); return; } } super.resolve(variable, context); }
@Override protected TemplateContext createContext(ITextViewer viewer, IRegion region) { TemplateContext context = null; TemplateContextType contextType= getContextType(viewer, region); if (contextType != null) { IDocument document= viewer.getDocument(); context = new BfTemplateContext(contextType, document, region.getOffset(), region.getLength()); } if (context == null) { return null; } try { String prefix = viewer.getDocument().get(region.getOffset(), region.getLength()); int i = 0; for (String param : parseParameters(prefix)) { context.setVariable("x" + (i++), param); } } catch (BadLocationException ex) { BfActivator.getDefault().logError("Prefix for Template could not be computed", ex); } return context; }
@Override int calculateValue(TemplateContext context) { String parseValue = null; if (this.isVariable) { parseValue = context.getVariable(this.value); if (parseValue == null) { String contextInfo = ""; if (context instanceof DocumentTemplateContext) { contextInfo = ": '" + ((DocumentTemplateContext) context).getKey() + "'"; } throw new VariableEvaluationException("Variable " + this.value + " is undefined in context"+ contextInfo); } parseValue = this.sign + parseValue; } else { parseValue = this.value; } try { return Integer.parseInt(parseValue); } catch (NumberFormatException ex) { throw new VariableEvaluationException(ex.getMessage(), ex); } }
@Override int calculateValue(TemplateContext context) { if (op == null) { return left.calculateValue(context); } switch (op) { case MULTPLY : return left.calculateValue(context) * right.calculateValue(context); case DIVIDE : return left.calculateValue(context) / right.calculateValue(context); case MODULO : return left.calculateValue(context) % right.calculateValue(context); case ADD : return left.calculateValue(context) + right.calculateValue(context); case SUBTRACT : return left.calculateValue(context) - right.calculateValue(context); default : throw new VariableEvaluationException("Invalid Operator" + op); } }
@SuppressWarnings("unchecked") @Override public void resolve(TemplateVariable variable, TemplateContext context) { if (!variable.getVariableType().getParams().isEmpty()) { String[] values = (String[]) variable.getVariableType().getParams().toArray(new String[0]); variable.setValues(values); variable.setUnambiguous(false); variable.setResolved(true); } else { super.resolve(variable, context); setEvaluationString(variable.getName()); } }
public static void insertAsTemplate(ITextViewer textViewer, final IRegion region, String templateText, CommandElement commandElement) { SnippetsCompletionProcessor snippetsCompletionProcessor = new SnippetsCompletionProcessor(); Template template = new SnippetTemplate(commandElement, templateText); TemplateContext context = snippetsCompletionProcessor.createContext(textViewer, region); SnippetTemplateProposal completionProposal = (SnippetTemplateProposal) snippetsCompletionProcessor .createProposal(template, context, region, 0); completionProposal.setTemplateProposals(new ICompletionProposal[] { completionProposal }); completionProposal.apply(textViewer, '0', SWT.NONE, region.getOffset()); Point selection = completionProposal.getSelection(textViewer.getDocument()); if (selection != null) { textViewer.setSelectedRange(selection.x, selection.y); textViewer.revealRange(selection.x, selection.y); } }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { List<String> params= variable.getVariableType().getParams(); String param; if (params.size() == 0) param= fDefaultType; else param= params.get(0); JavaContext jc= (JavaContext) context; MultiVariable mv= (MultiVariable) variable; String reference= jc.addImport(param); mv.setValue(reference); mv.setUnambiguous(true); }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { variable.setUnambiguous(true); variable.setValue(""); //$NON-NLS-1$ if (context instanceof JavaContext) { JavaContext jc= (JavaContext) context; List<String> params= variable.getVariableType().getParams(); if (params.size() > 0) { for (Iterator<String> iterator= params.iterator(); iterator.hasNext();) { String typeName= iterator.next(); jc.addImport(typeName); } } } else { super.resolve(variable, context); } }
@Override protected String resolve(TemplateContext context) { IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD); if (element == null) return null; IMethod method= (IMethod) element; try { String[] arguments= method.getParameterNames(); StringBuffer buffer= new StringBuffer(); for (int i= 0; i < arguments.length; i++) { if (i > 0) buffer.append(", "); //$NON-NLS-1$ buffer.append(arguments[i]); } return buffer.toString(); } catch (JavaModelException e) { return null; } }
/** * Returns template string to insert. * * @return String to insert or null if none is to be inserted */ String getTemplateString() { String templateString = null; Template template = getSelectedTemplate(); if (template != null) { TemplateContextType contextType = GlassmakerUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CardContextType.CONTEXT_TYPE); IDocument document = new Document(); TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0); try { TemplateBuffer buffer = context.evaluate(template); templateString = buffer.getString(); } catch (Exception e) { GlassmakerUIPlugin.logError("Could not create template for new html", e); } } return templateString; }
/** * Formats the template buffer. * @param buffer * @param context * @throws BadLocationException */ public void format(TemplateBuffer buffer, TemplateContext context) throws BadLocationException { try { VariableTracker tracker= new VariableTracker(buffer); IDocument document= tracker.getDocument(); internalFormat(document, context); convertLineDelimiters(document); if (!(context instanceof JavaDocContext) && !isReplacedAreaEmpty(context)) trimStart(document); tracker.updateBuffer(); } catch (MalformedTreeException e) { throw new BadLocationException(); } }
private boolean isReplacedAreaEmpty(TemplateContext context) { // don't trim the buffer if the replacement area is empty // case: surrounding empty lines with block if (context instanceof DocumentTemplateContext) { DocumentTemplateContext dtc= (DocumentTemplateContext) context; if (dtc.getStart() == dtc.getCompletionOffset()) try { IDocument document= dtc.getDocument(); int lineOffset= document.getLineInformationOfOffset(dtc.getStart()).getOffset(); //only if we are at the beginning of the line if (lineOffset != dtc.getStart()) return false; //Does the selection only contain whitespace characters? if (document.get(dtc.getStart(), dtc.getEnd() - dtc.getStart()).trim().length() == 0) return true; } catch (BadLocationException x) { // ignore - this may happen when the document was modified after the initial invocation, and the // context does not track the changes properly - don't trim in that case return true; } } return false; }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { if (!(variable instanceof MultiVariable)) { super.resolve(variable, context); return; } MultiVariable mv= (MultiVariable) variable; List<String> params= variable.getVariableType().getParams(); if (params.isEmpty()) { super.resolve(variable, context); return; } JavaContext jc= (JavaContext) context; String reference= params.get(0); TemplateVariable refVar= jc.getTemplateVariable(reference); if (refVar instanceof JavaVariable) { JavaVariable jvar= (JavaVariable) refVar; resolve(mv, jvar, jc); return; } super.resolve(variable, context); }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { if (variable instanceof MultiVariable) { JavaContext jc= (JavaContext) context; JavaVariable jv= (JavaVariable) variable; Variable[] iterables= getVariables(jc); if (iterables.length > 0) { jv.setChoices(iterables); jc.markAsUsed(iterables[0].getName()); if (iterables.length > 1) variable.setUnambiguous(false); else variable.setUnambiguous(isUnambiguous(context)); return; } } super.resolve(variable, context); }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { if (context instanceof JavaStatementPostfixContext && variable instanceof JavaVariable) { JavaStatementPostfixContext c = (JavaStatementPostfixContext) context; JavaVariable jv = (JavaVariable) variable; List<String> params = variable.getVariableType().getParams(); if (!params.contains(HIDE_FLAG)) { jv.setValue(resolve(context)); } else { jv.setValues(new String[] { "", resolve(context) }); // We hide the value from the output } jv.setParamType(c.getInnerExpressionTypeSignature()); jv.setResolved(true); jv.setUnambiguous(true); return; } super.resolve(variable, context); }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { variable.setUnambiguous(true); variable.setValue(""); //$NON-NLS-1$ if (context instanceof JavaContext) { JavaContext jc= (JavaContext) context; List<String> params= variable.getVariableType().getParams(); if (params.size() > 0) { for (Iterator<String> iterator= params.iterator(); iterator.hasNext();) { String qualifiedMemberName= iterator.next(); jc.addStaticImport(qualifiedMemberName); } } } else { super.resolve(variable, context); } }
@Override public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { ITextSelection selection = (ITextSelection) viewer .getSelectionProvider().getSelection(); // adjust offset to end of normalized selection if (selection.getOffset() == offset) { offset = selection.getOffset() + selection.getLength(); } String prefix = extractPrefix(viewer, offset); Region region = new Region(offset - prefix.length(), prefix.length()); TemplateContext context = createContext(viewer, region); if (context == null) { return new ICompletionProposal[0]; } context.setVariable("selection", selection.getText()); // name of the selection variables {line, word_selection //$NON-NLS-1$ Template[] templates = getTemplates(context.getContextType().getId()); List<ICompletionProposal> matches = new ArrayList<>(); for (Template template : templates) { try { context.getContextType().validate(template.getPattern()); } catch (TemplateException e) { continue; } if (!prefix.equals("") && prefix.charAt(0) == '<') { //$NON-NLS-1$ prefix = prefix.substring(1); } if (!prefix.equals("") //$NON-NLS-1$ && (template.getName().startsWith(prefix) && template .matches(prefix, context.getContextType().getId()))) { matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix))); } } return matches.toArray(new ICompletionProposal[matches.size()]); }
public List<ICompletionProposal> getAllTemplates(ITextViewer viewer, int offset) { List<ICompletionProposal> result = new ArrayList<>(); String prefix = extractPrefix(viewer, offset); Region region = new Region(offset - prefix.length(), prefix.length()); TemplateContext context = createContext(viewer, region); Template[] templates = getTemplates(context.getContextType().getId()); for (Template template : templates) { result.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix))); } return result; }
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection(); // adjust offset to end of normalized selection if (selection.getOffset() == offset) offset= selection.getOffset() + selection.getLength(); String prefix= extractPrefix(viewer, offset); Region region= new Region(offset - prefix.length(), prefix.length()); TemplateContext context= createContext(viewer, region); if (context == null) return new ICompletionProposal[0]; context.setVariable("selection", selection.getText()); // name of the selection variables {line, word}_selection //$NON-NLS-1$ Template[] templates= getTemplates(context.getContextType().getId()); List matches= new ArrayList(); for (int i= 0; i < templates.length; i++) { Template template= templates[i]; try { context.getContextType().validate(template.getPattern()); } catch (TemplateException e) { continue; } if (template.matches(prefix, context.getContextType().getId())) matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix))); } Collections.sort(matches, fgProposalComparator); return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]); }
private boolean isSelectionBasedMatch(Template template, TemplateContext context) { String pattern = template.getPattern(); Set<String> vars = new HashSet<String>(); Matcher matcher = VARIABLE_PATTERN.matcher(pattern); while (matcher.find()) { String variableName = matcher.group(1); if (vars.add(variableName)) { String variable = context.getVariable(variableName); if (variable != null && variable.length() > 0) { return true; } } } return false; }
@Override protected void createTemplates(TemplateContext templateContext, ContentAssistContext context, ITemplateAcceptor acceptor) { TemplateContextType contextType = templateContext.getContextType(); Template[] templates = templateStore.getTemplates(contextType.getId()); for (Template template : templates) { if (!acceptor.canAcceptMoreTemplates()) return; if (validate(template, templateContext)) { acceptor.accept(createProposal(template, templateContext, context, getImage(template), getRelevance(template))); } } }
@Override public void resolve(TemplateVariable variable, TemplateContext templateContext) { XtextTemplateContext castedContext = (XtextTemplateContext) templateContext; List<String> names = resolveValues(variable, castedContext); String[] bindings = names.toArray(new String[names.size()]); if (bindings.length != 0) variable.setValues(bindings); if (bindings.length > 1) variable.setUnambiguous(false); else variable.setUnambiguous(isUnambiguous(castedContext)); variable.setResolved(true); }
public void createTemplates(ContentAssistContext context, ITemplateAcceptor acceptor) { if (!acceptor.canAcceptMoreTemplates()) return; TemplateContext[] templateContexts = createTemplateContexts(context); if (templateContexts == null || templateContexts.length == 0) return; ITemplateAcceptor nullSafe = new NullSafeTemplateAcceptor(acceptor); for(TemplateContext templateContext: templateContexts) { if (!nullSafe.canAcceptMoreTemplates()) return; templateContext.setVariable("selection", context.getSelectedText()); // name of the selection variables {line, word}_selection //$NON-NLS-1$ createTemplates(templateContext, context, nullSafe); } }
protected boolean validate(Template template, TemplateContext context) { try { context.getContextType().validate(template.getPattern()); } catch(TemplateException e) { return false; } return true; }