Search in sources :

Example 41 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project flux by eclipse.

the class StubUtility method fixEmptyVariables.

// remove lines for empty variables
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++) {
        // look if Javadoc tags have to be added
        TemplateVariable position = findVariable(buffer, variables[i]);
        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();
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IRegion(org.eclipse.jface.text.IRegion) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 42 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project dbeaver by serge-rider.

the class SQLEntityResolver method resolveTables.

static void resolveTables(DBRProgressMonitor monitor, DBCExecutionContext executionContext, TemplateContext context, List<DBSEntity> entities) throws DBException {
    TemplateVariable schemaVariable = ((SQLContext) context).getTemplateVariable(SQLContainerResolver.VAR_NAME_SCHEMA);
    TemplateVariable catalogVariable = ((SQLContext) context).getTemplateVariable(SQLContainerResolver.VAR_NAME_CATALOG);
    String catalogName = catalogVariable == null ? null : catalogVariable.getDefaultValue();
    String schemaName = schemaVariable == null ? null : schemaVariable.getDefaultValue();
    DBSObjectContainer objectContainer = DBUtils.getAdapter(DBSObjectContainer.class, executionContext.getDataSource());
    if (objectContainer == null) {
        return;
    }
    if (!CommonUtils.isEmpty(catalogName) || !CommonUtils.isEmpty(schemaName)) {
        // Find container for specified schema/catalog
        objectContainer = (DBSObjectContainer) DBUtils.getObjectByPath(monitor, executionContext, objectContainer, catalogName, schemaName, null);
    } else {
        objectContainer = DBUtils.getSelectedObject(executionContext, DBSObjectContainer.class);
    }
    if (objectContainer != null) {
        makeProposalsFromChildren(monitor, objectContainer, entities);
    }
}
Also used : DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable)

Example 43 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project titan.EclipsePlug-ins by eclipse.

the class TITANTemplateContext method positionsToVariables.

private static void positionsToVariables(final List<RangeMarker> positions, final TemplateVariable[] variables) {
    Iterator<RangeMarker> iterator = positions.iterator();
    for (int i = 0; i != variables.length; i++) {
        TemplateVariable variable = variables[i];
        int[] offsets = new int[variable.getOffsets().length];
        for (int j = 0; j != offsets.length; j++) {
            offsets[j] = ((TextEdit) iterator.next()).getOffset();
        }
        variable.setOffsets(offsets);
    }
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) RangeMarker(org.eclipse.text.edits.RangeMarker)

Example 44 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project titan.EclipsePlug-ins by eclipse.

the class TITANTemplateContext method evaluate.

@Override
public TemplateBuffer evaluate(final Template template) throws BadLocationException, TemplateException {
    if (!canEvaluate(template)) {
        return null;
    }
    TemplateTranslator translator = new TemplateTranslator();
    TemplateBuffer buffer = translator.translate(template);
    getContextType().resolve(buffer, this);
    if (isReadOnly()) {
        // if it is read only we should not modify it
        return buffer;
    }
    // calculate base indentation prefix
    IDocument document = getDocument();
    String prefixString = "";
    String delimeter = null;
    try {
        IRegion lineRegion = document.getLineInformationOfOffset(getCompletionOffset());
        int firstCharLocation = FirstCharAction.firstVisibleCharLocation(document, lineRegion);
        if (firstCharLocation != -1) {
            prefixString = document.get(lineRegion.getOffset(), firstCharLocation - lineRegion.getOffset());
        }
        delimeter = document.getLineDelimiter(document.getLineOfOffset(getCompletionOffset()));
    } catch (BadLocationException e) {
        ErrorReporter.logExceptionStackTrace(e);
    }
    TemplateVariable[] variables = buffer.getVariables();
    // apply the base indentation prefix to every line but the first
    IDocument temporalDocument = new Document(buffer.getString());
    MultiTextEdit edit = new MultiTextEdit(0, temporalDocument.getLength());
    List<RangeMarker> positions = variablesToPositions(variables);
    for (int i = temporalDocument.getNumberOfLines() - 1; i >= 0; i--) {
        edit.addChild(new InsertEdit(temporalDocument.getLineOffset(i), prefixString));
    }
    edit.addChildren(positions.toArray(new TextEdit[positions.size()]));
    // replace line delimeters with the ones at the insertion
    String delimeterZero = temporalDocument.getLineDelimiter(0);
    if (delimeter != null && delimeterZero != null && !delimeter.equals(delimeterZero)) {
        FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(temporalDocument);
        int startOffset = 0;
        IRegion region = adapter.find(startOffset, delimeterZero, true, false, false, false);
        while (region != null) {
            edit.addChild(new ReplaceEdit(region.getOffset(), region.getLength(), delimeter));
            startOffset = region.getOffset() + region.getLength();
            region = adapter.find(startOffset, delimeterZero, true, false, false, false);
        }
    }
    edit.apply(temporalDocument, TextEdit.UPDATE_REGIONS);
    positionsToVariables(positions, variables);
    buffer.setContent(temporalDocument.get(), variables);
    return buffer;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) RangeMarker(org.eclipse.text.edits.RangeMarker) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter)

Aggregations

TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)44 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)30 IDocument (org.eclipse.jface.text.IDocument)14 Test (org.junit.Test)14 Document (org.eclipse.jface.text.Document)13 BadLocationException (org.eclipse.jface.text.BadLocationException)11 Template (org.eclipse.jface.text.templates.Template)10 TemplateException (org.eclipse.jface.text.templates.TemplateException)10 CoreException (org.eclipse.core.runtime.CoreException)9 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 ArrayList (java.util.ArrayList)5 IRegion (org.eclipse.jface.text.IRegion)4 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)3 MultiVariable (org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable)3 CodeTemplateContext (org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext)3 CodeGenerationTemplate (org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashSet (java.util.HashSet)2