@Override protected void validateVariables(TemplateVariable[] variables) throws TemplateException { ArrayList<String> required = new ArrayList<>(5); String contextName = getId(); if (NEWTYPE_CONTEXTTYPE.equals(contextName)) { required.add(PACKAGE_DECLARATION); required.add(TYPE_DECLARATION); } for (int i = 0; i < variables.length; i++) { String type = variables[i].getType(); if (getResolver(type) == null) { String unknown = BasicElementLabels.getJavaElementName(type); throw new TemplateException(Messages .format(JavaTemplateMessages.CodeTemplateContextType_validate_unknownvariable, unknown)); } required.remove(type); } if (!required.isEmpty()) { String missing = BasicElementLabels.getJavaElementName(required.get(0)); throw new TemplateException( Messages.format(JavaTemplateMessages.CodeTemplateContextType_validate_missingvariable, missing)); } super.validateVariables(variables); }
@Override public List<String> resolveValues(TemplateVariable variable, XtextTemplateContext castedContext) { String enumerationName = (String) variable.getVariableType() .getParams().iterator().next(); Grammar grammar = getGrammar(castedContext); if (grammar == null) return Collections.emptyList(); EEnum enumeration = (EEnum) getEClassifierForGrammar(enumerationName, grammar); if (enumeration == null) { return Collections.emptyList(); } return Lists.transform(enumeration.getELiterals(), new Function<EEnumLiteral, String>() { public String apply(EEnumLiteral enumLiteral) { return enumLiteral.getLiteral(); } }); }
@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 void validateVariables(TemplateVariable[] variables) throws TemplateException { ArrayList<String> required = new ArrayList<String>(5); String contextName = getId(); if (NEWTYPE_CONTEXTTYPE.equals(contextName)) { required.add(PACKAGE_DECLARATION); required.add(TYPE_DECLARATION); } for (int i = 0; i < variables.length; i++) { String type = variables[i].getType(); if (getResolver(type) == null) { String unknown = BasicElementLabels.getJavaElementName(type); throw new TemplateException( Messages.format( JavaTemplateMessages.CodeTemplateContextType_validate_unknownvariable, unknown)); } required.remove(type); } if (!required.isEmpty()) { String missing = BasicElementLabels.getJavaElementName(required.get(0)); throw new TemplateException( Messages.format( JavaTemplateMessages.CodeTemplateContextType_validate_missingvariable, missing)); } super.validateVariables(variables); }
@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); }
@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()); } }
@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 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 void validateVariables(TemplateVariable[] variables) throws TemplateException { ArrayList<String> required= new ArrayList<String>(5); String contextName= getId(); if (NEWTYPE_CONTEXTTYPE.equals(contextName)) { required.add(PACKAGE_DECLARATION); required.add(TYPE_DECLARATION); } for (int i= 0; i < variables.length; i++) { String type= variables[i].getType(); if (getResolver(type) == null) { String unknown= BasicElementLabels.getJavaElementName(type); throw new TemplateException(Messages.format(JavaTemplateMessages.CodeTemplateContextType_validate_unknownvariable, unknown)); } required.remove(type); } if (!required.isEmpty()) { String missing= BasicElementLabels.getJavaElementName(required.get(0)); throw new TemplateException(Messages.format(JavaTemplateMessages.CodeTemplateContextType_validate_missingvariable, missing)); } super.validateVariables(variables); }
@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); }
private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException { IDocument doc= new Document(buffer.getString()); int nLines= doc.getNumberOfLines(); MultiTextEdit edit= new MultiTextEdit(); HashSet<Integer> removedLines= new HashSet<Integer>(); for (int i= 0; i < variables.length; i++) { TemplateVariable position= findVariable(buffer, variables[i]); // look if Javadoc tags have to be added if (position == null || position.getLength() > 0) { continue; } int[] offsets= position.getOffsets(); for (int k= 0; k < offsets.length; k++) { int line= doc.getLineOfOffset(offsets[k]); IRegion lineInfo= doc.getLineInformation(line); int offset= lineInfo.getOffset(); String str= doc.get(offset, lineInfo.getLength()); if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) { int nextStart= doc.getLineOffset(line + 1); edit.addChild(new DeleteEdit(offset, nextStart - offset)); } } } edit.apply(doc, 0); return doc.get(); }
@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) { if (variable instanceof MultiVariable) { JavaContext jc= (JavaContext) context; JavaVariable jv= (JavaVariable) variable; Variable[] iterables= getLocalVariables(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); }
public TweakedProposalPosition(TemplateVariable templateVariable, String positionCategory, FtcDocumentTemplateContext context, int offset, int length, ICompletionProposal[] proposals) { super(context.getDocument(), offset, length, proposals); this.variable = templateVariable; this.context = context; position = new Position(offset, length); try { getDocument().addPosition(positionCategory, position); } catch (Exception e) { throw new RuntimeException(e); } }
private ICompletionProposal[] createPositionBasedProposals(int offset, TemplateVariable variable, ICompletionProposal[] values) { int length = variable.getLength(); ICompletionProposal[] proposals = new ICompletionProposal[values.length]; for (int j = 0; j < values.length; j++) proposals[j] = new PositionBasedCompletionProposal(values[j].getDisplayString(), position, length); return proposals; }
private int getCaretOffset(TemplateBuffer buffer) { TemplateVariable[] variables = buffer.getVariables(); for (int i = 0; i != variables.length; i++) { TemplateVariable variable = variables[i]; if (variable.getType().equals(GlobalTemplateVariables.Cursor.NAME)) return variable.getOffsets()[0]; } return buffer.getString().length(); }
private static TemplateVariable findVariable(TemplateBuffer buffer, String variable) { TemplateVariable[] positions = buffer.getVariables(); for (int i = 0; i < positions.length; i++) { TemplateVariable curr = positions[i]; if (variable.equals(curr.getType())) { return curr; } } return null; }
@Override public List<String> resolveValues(final TemplateVariable variable, final XtextTemplateContext templateContext) { final List<String> result = Lists.newArrayList(); IDocument document = templateContext.getDocument(); final Object obj = variable.getVariableType().getParams().iterator().next(); if (obj instanceof String) { final String variableName = (String) obj; final IXtextDocument xtextDocument = (IXtextDocument) document; IFile file = xtextDocument.getAdapter(IFile.class); if ("package".equals(variableName) && document instanceof IXtextDocument) { if (file != null && file.getParent() instanceof IFolder) { IJavaProject javaProject = JavaCore.create(file.getProject()); try { IPackageFragment packageFragment = javaProject.findPackageFragment(file.getParent().getFullPath()); result.add(packageFragment.getElementName()); } catch (JavaModelException e) { LOGGER.error("Could not determine package for file of given document"); } } } else if ("catalog".equals(variableName)) { final String fileName = file.getName(); result.add(fileName.indexOf('.') > 0 ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName); } } return Lists.newArrayList(Iterables.filter(result, Predicates.notNull())); }
@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); }
@Override public List<String> resolveValues(TemplateVariable variable, XtextTemplateContext castedContext) { String abbreviatedCrossReference = (String) variable.getVariableType().getParams().iterator().next(); int dotIndex = abbreviatedCrossReference.lastIndexOf('.'); if (dotIndex <= 0) { log.error("CrossReference '" + abbreviatedCrossReference + "' could not be resolved."); //$NON-NLS-1$ //$NON-NLS-2$ return Collections.emptyList(); } String[] classReferencePair = new String[] { abbreviatedCrossReference.substring(0, dotIndex), abbreviatedCrossReference.substring(dotIndex + 1) }; Grammar grammar = getGrammar(castedContext); if (grammar == null) { return Collections.emptyList(); } EReference reference = getReference(classReferencePair[0], classReferencePair[1], grammar); if (reference == null) { log.debug("CrossReference to class '" + classReferencePair[0] + "' and reference '" + classReferencePair[1] //$NON-NLS-1$ //$NON-NLS-2$ + "' could not be resolved."); //$NON-NLS-1$ return Collections.emptyList(); } IScope scope = castedContext.getScopeProvider().getScope( castedContext.getContentAssistContext().getCurrentModel(), reference); Iterable<IEObjectDescription> linkingCandidates = queryScope(scope); List<String> names = new ArrayList<String>(); for (IEObjectDescription eObjectDescription : linkingCandidates) { names.add(qualifiedNameConverter.toString(eObjectDescription.getName())); } return names; }
@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 protected void validateVariables(TemplateVariable[] variables) throws TemplateException { // check for multiple cursor variables for (int i = 0; i < variables.length; i++) { TemplateVariable var = variables[i]; if (var.getType().equals(GlobalTemplateVariables.Cursor.NAME)) { if (var.getOffsets().length > 1) { throw new TemplateException( JavaTemplateMessages.ContextType_error_multiple_cursor_variables); } } } }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { variable.setUnambiguous(false); if (variable instanceof JavaVariable) { JavaContext jc = (JavaContext) context; JavaVariable jv = (JavaVariable) variable; List<String> params = variable.getVariableType().getParams(); if (params.size() > 0) { fProposals = new String[params.size()]; int i = 0; for (Iterator<String> iterator = params.iterator(); iterator.hasNext(); ) { String param = iterator.next(); fProposals[i] = param; i++; } jv.setChoices(fProposals); jv.setCurrentChoice(fProposals[0]); jc.markAsUsed(jv.getDefaultValue()); } else { fProposals = new String[] {variable.getDefaultValue()}; super.resolve(variable, context); return; } } else super.resolve(variable, context); }
@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); int index = 0; if (params.size() > 1) { String indexParam = params.get(1); try { index = Integer.parseInt(indexParam); } catch (NumberFormatException x) { } } TemplateVariable refVar = jc.getTemplateVariable(reference); if (refVar instanceof JavaVariable) { JavaVariable jvar = (JavaVariable) refVar; resolve(mv, jvar, index, jc); return; } super.resolve(variable, context); }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { if (variable instanceof JavaVariable) { JavaContext jc = (JavaContext) context; JavaVariable jv = (JavaVariable) variable; List<String> params = variable.getVariableType().getParams(); if (params.size() == 0) { fVariables = getVisibleVariables(fDefaultType, jc); jv.setParamType(fDefaultType); } else if (params.size() == 1) { String type = params.get(0); fVariables = getVisibleVariables(type, jc); jv.setParamType(type); } else { ArrayList<Variable> variables = new ArrayList<Variable>(); for (Iterator<String> iterator = params.iterator(); iterator.hasNext(); ) { variables.addAll(Arrays.asList(getVisibleVariables(iterator.next(), jc))); } fVariables = variables.toArray(new Variable[variables.size()]); // set to default type, a template which references to the type // of _the_ parameter will not correctly work anyway jv.setParamType(fDefaultType); } if (fVariables.length > 0) { jv.setChoices(fVariables); jc.markAsUsed(jv.getDefaultValue()); } else { super.resolve(variable, context); return; } if (fVariables.length > 1) variable.setUnambiguous(false); else variable.setUnambiguous(isUnambiguous(context)); } else super.resolve(variable, context); }
/** * Restores any decorated regions and updates the buffer's variable offsets. * * @return the buffer. * @throws MalformedTreeException * @throws BadLocationException */ public TemplateBuffer updateBuffer() throws MalformedTreeException, BadLocationException { checkState(); TemplateVariable[] variables = fBuffer.getVariables(); try { removeRangeMarkers(fPositions, fDocument, variables); } catch (BadPositionCategoryException x) { Assert.isTrue(false); } fBuffer.setContent(fDocument.get(), variables); fDocument = null; return fBuffer; }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { if (variable instanceof MultiVariable) { JavaContext jc = (JavaContext) context; MultiVariable mv = (MultiVariable) variable; Variable[] iterables = getVariablesInContextScope(jc); if (iterables.length > 0) { for (int i = 0; i < iterables.length; i++) mv.setChoices(iterables[i], iterables[i].getMemberTypeNames()); TemplateVariable master = jc.getTemplateVariable(fMasterName); if (master instanceof MultiVariable) { final MultiVariable masterMv = (MultiVariable) master; jc.addDependency(masterMv, mv); mv.setKey(masterMv.getCurrentChoice()); } if (iterables.length > 1 || iterables.length == 1 && mv.getChoices().length > 1) variable.setUnambiguous(false); else variable.setUnambiguous(isUnambiguous(context)); return; } } super.resolve(variable, context); }
@Override public void resolve(TemplateVariable variable, TemplateContext context) { if (variable instanceof MultiVariable) { JavaContext jc = (JavaContext) context; MultiVariable mv = (MultiVariable) variable; Variable[] iterables = getLocalVariables(jc); if (iterables.length > 0) { for (int i = 0; i < iterables.length; i++) { String[] elements = jc.suggestVariableNames(iterables[i].getMemberTypeNames()[0]); mv.setChoices(iterables[i], elements); } TemplateVariable master = jc.getTemplateVariable(fMasterName); if (master instanceof MultiVariable) { final MultiVariable masterMv = (MultiVariable) master; jc.addDependency(masterMv, mv); mv.setKey(masterMv.getCurrentChoice()); } jc.markAsUsed(mv.getDefaultValue()); if (iterables.length > 1 || iterables.length == 1 && mv.getChoices().length > 1) variable.setUnambiguous(false); else variable.setUnambiguous(isUnambiguous(context)); return; } } super.resolve(variable, context); }
private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException { IDocument doc = new Document(buffer.getString()); int nLines = doc.getNumberOfLines(); MultiTextEdit edit = new MultiTextEdit(); HashSet<Integer> removedLines = new HashSet<Integer>(); for (int i = 0; i < variables.length; i++) { TemplateVariable position = findVariable(buffer, variables[i]); // look if Javadoc tags have to be added if (position == null || position.getLength() > 0) { continue; } int[] offsets = position.getOffsets(); for (int k = 0; k < offsets.length; k++) { int line = doc.getLineOfOffset(offsets[k]); IRegion lineInfo = doc.getLineInformation(line); int offset = lineInfo.getOffset(); String str = doc.get(offset, lineInfo.getLength()); if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) { int nextStart = doc.getLineOffset(line + 1); edit.addChild(new DeleteEdit(offset, nextStart - offset)); } } } edit.apply(doc, 0); return doc.get(); }