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();
}
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);
}
}
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);
}
}
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;
}
Aggregations