use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class TemplateEngine method complete.
/**
* Inspects the context of the compilation unit around <code>completionPosition</code>
* and feeds the collector with proposals.
* @param viewer the text viewer
* @param completionPosition the context position in the document of the text viewer
* @param compilationUnit the compilation unit (may be <code>null</code>)
*/
public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
IDocument document = viewer.getDocument();
if (!(fContextType instanceof CompilationUnitContextType))
return;
Point selection = viewer.getSelectedRange();
Position position = new Position(completionPosition, selection.y);
// remember selected text
String selectedText = null;
if (selection.y != 0) {
try {
selectedText = document.get(selection.x, selection.y);
document.addPosition(position);
fPositions.put(document, position);
} catch (BadLocationException e) {
}
}
CompilationUnitContext context = ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit);
//$NON-NLS-1$
context.setVariable("selection", selectedText);
int start = context.getStart();
int end = context.getEnd();
IRegion region = new Region(start, end - start);
Template[] templates = JavaPlugin.getDefault().getTemplateStore().getTemplates();
if (selection.y == 0) {
for (int i = 0; i != templates.length; i++) {
Template template = templates[i];
if (context.canEvaluate(template)) {
fProposals.add(new TemplateProposal(template, context, region, getImage()));
}
}
} else {
if (context.getKey().length() == 0)
context.setForceEvaluation(true);
boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
for (int i = 0; i != templates.length; i++) {
Template template = templates[i];
if (context.canEvaluate(template) && (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1))) {
fProposals.add(new TemplateProposal(templates[i], context, region, getImage()));
}
}
}
}
use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class MethodDeclarationCompletionProposal method updateReplacementString.
/* (non-Javadoc)
* @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
*/
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject());
boolean addComments = settings.createComments;
String[] empty = new String[0];
String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
String declTypeName = fType.getTypeQualifiedName('.');
boolean isInterface = fType.isInterface();
StringBuffer buf = new StringBuffer();
if (addComments) {
String comment = CodeGeneration.getMethodComment(fType.getCompilationUnit(), declTypeName, fMethodName, empty, empty, fReturnTypeSig, empty, null, lineDelim);
if (comment != null) {
buf.append(comment);
buf.append(lineDelim);
}
}
if (fReturnTypeSig != null) {
if (!isInterface) {
//$NON-NLS-1$
buf.append("private ");
}
} else {
if (fType.isEnum())
//$NON-NLS-1$
buf.append("private ");
else
//$NON-NLS-1$
buf.append("public ");
}
if (fReturnTypeSig != null) {
buf.append(Signature.toString(fReturnTypeSig));
}
buf.append(' ');
buf.append(fMethodName);
if (isInterface) {
//$NON-NLS-1$
buf.append("();");
buf.append(lineDelim);
} else {
//$NON-NLS-1$
buf.append("() {");
buf.append(lineDelim);
String body = CodeGeneration.getMethodBodyContent(fType.getCompilationUnit(), declTypeName, fMethodName, fReturnTypeSig == null, "", //$NON-NLS-1$
lineDelim);
if (body != null) {
buf.append(body);
buf.append(lineDelim);
}
//$NON-NLS-1$
buf.append("}");
buf.append(lineDelim);
}
String stub = buf.toString();
// use the code formatter
IRegion region = document.getLineInformationOfOffset(getReplacementOffset());
int lineStart = region.getOffset();
int indent = Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth);
String replacement = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, lineDelim, fType.getJavaProject());
if (replacement.endsWith(lineDelim)) {
replacement = replacement.substring(0, replacement.length() - lineDelim.length());
}
setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement));
return true;
}
use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class CodeTemplateContext method changeLineDelimiter.
private static String changeLineDelimiter(String code, String lineDelim) {
try {
ILineTracker tracker = new DefaultLineTracker();
tracker.set(code);
int nLines = tracker.getNumberOfLines();
if (nLines == 1) {
return code;
}
StringBuffer buf = new StringBuffer();
for (int i = 0; i < nLines; i++) {
if (i != 0) {
buf.append(lineDelim);
}
IRegion region = tracker.getLineInformation(i);
String line = code.substring(region.getOffset(), region.getOffset() + region.getLength());
buf.append(line);
}
return buf.toString();
} catch (BadLocationException e) {
// can not happen
return code;
}
}
use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class JavaContext method getIndentation.
/**
* Returns the indentation level at the position of code completion.
*
* @return the indentation level at the position of the code completion
*/
private int getIndentation() {
int start = getStart();
IDocument document = getDocument();
try {
IRegion region = document.getLineInformationOfOffset(start);
String lineContent = document.get(region.getOffset(), region.getLength());
IJavaProject project = getJavaProject();
return Strings.computeIndentUnits(lineContent, project);
} catch (BadLocationException e) {
return 0;
}
}
use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class IndentUtil method addIndent.
/**
* Indents line <code>line</code> in <code>document</code> with <code>indent</code>.
* Leaves leading comment signs alone.
*
* @param document the document
* @param line the line
* @param indent the indentation to insert
* @param commentlines
* @throws BadLocationException on concurrent document modification
*/
private static void addIndent(IDocument document, int line, CharSequence indent, boolean[] commentlines, int relative) throws BadLocationException {
IRegion region = document.getLineInformation(line);
int insert = region.getOffset();
int endOffset = region.getOffset() + region.getLength();
// go behind line comments
if (!commentlines[relative]) {
while (insert < endOffset - 2 && document.get(insert, 2).equals(SLASHES)) insert += 2;
}
// insert indent
document.replace(insert, 0, indent.toString());
}
Aggregations