use of org.eclipse.jface.text.ITypedRegion in project linuxtools by eclipse.
the class STPAutoEditStrategy method smartIndentAfterOpeningBracket.
private void smartIndentAfterOpeningBracket(IDocument d, DocumentCommand c) {
if (c.offset < 1 || d.getLength() == 0)
return;
int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);
try {
STPHeuristicScanner scanner = new STPHeuristicScanner(d);
ITypedRegion partition = TextUtilities.getPartition(d, fPartitioning, p, false);
if (STPPartitionScanner.STP_CONDITIONAL.equals(partition.getType())) {
scanner = new STPHeuristicScanner(d, fPartitioning, STPPartitionScanner.STP_CONDITIONAL);
}
// current line
int line = d.getLineOfOffset(c.offset);
int lineOffset = d.getLineOffset(line);
// make sure we don't have any leading comments etc.
if (!d.get(lineOffset, c.offset - lineOffset).trim().isEmpty())
return;
// Line of last C code
int pos = scanner.findNonWhitespaceBackward(p, STPHeuristicScanner.UNBOUND);
if (pos == -1)
return;
int lastLine = d.getLineOfOffset(pos);
// Only shift if the last C line is further up and is a braceless block candidate
if (lastLine < line) {
STPIndenter indenter = new STPIndenter(d, scanner, fProject);
StringBuilder indent = indenter.computeIndentation(p, true);
String toDelete = d.get(lineOffset, c.offset - lineOffset);
if (indent != null && !indent.toString().equals(toDelete)) {
c.text = indent.append(c.text).toString();
c.length += c.offset - lineOffset;
c.offset = lineOffset;
}
}
} catch (BadLocationException e) {
IDEPlugin.log(e);
}
}
use of org.eclipse.jface.text.ITypedRegion in project linuxtools by eclipse.
the class STPAutoEditStrategy method inStringOrComment.
private boolean inStringOrComment(IDocument d, DocumentCommand c) {
int docLength = d.getLength();
if (c.offset == -1 || docLength == 0)
return false;
try {
ITypedRegion partition = TextUtilities.getPartition(d, fPartitioning, c.offset, false);
String partitionType = partition.getType();
if (c.offset > 0 && (STPPartitionScanner.STP_COMMENT.equals(partitionType) || STPPartitionScanner.STP_MULTILINE_COMMENT.equals(partitionType) || STPPartitionScanner.STP_STRING.equals(partitionType))) {
return true;
}
IRegion lineInfo = d.getLineInformationOfOffset(c.offset);
int offset = lineInfo.getOffset();
boolean inChar = false;
boolean inString = false;
boolean inComment = false;
for (int i = offset; i < c.offset; ++i) {
char ch = d.getChar(i);
switch(ch) {
case '\"':
if (!inChar)
inString = !inString;
break;
case '\'':
if (!inString)
inChar = !inChar;
break;
case '\\':
++i;
break;
case '/':
if (!inString && !inChar) {
ch = d.getChar(i + 1);
if (ch == '/')
// We have a line comment
return true;
else if (ch == '*')
inComment = true;
}
break;
case '#':
if (!inString && !inChar)
return true;
break;
case '*':
if (!inString && !inChar) {
if (inComment) {
ch = d.getChar(i + 1);
if (ch == '/')
inComment = false;
}
}
break;
}
}
return inString || inChar || inComment;
} catch (BadLocationException e) {
IDEPlugin.log(e);
}
return false;
}
use of org.eclipse.jface.text.ITypedRegion in project linuxtools by eclipse.
the class STPCompletionProcessor method getHoverInfo.
@Override
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
String documentation = null;
try {
String keyword = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
int offset = hoverRegion.getOffset();
IDocument document = textViewer.getDocument();
if (getPrecedingToken(document, offset - 1).tokenString.equals(PROBE_KEYWORD.trim())) {
documentation = ManpageCacher.getDocumentation(TapsetItemType.PROBE, keyword);
} else {
ITypedRegion partition = ((IDocumentExtension3) document).getPartition(STPProbeScanner.STP_PROBE_PARTITIONING, offset, false);
if (partition.getType() == STPProbeScanner.STP_PROBE) {
if (isFunctionRegion(document, hoverRegion)) {
documentation = ManpageCacher.getDocumentation(TapsetItemType.FUNCTION, keyword);
} else {
String probe = getProbe(document, offset);
if (stpMetadataSingleton.isVariableInProbe(probe, keyword)) {
documentation = ManpageCacher.getDocumentation(TapsetItemType.PROBEVAR, probe, keyword);
}
}
}
}
} catch (BadLocationException | BadPartitioningException e) {
// Bad hover location/scenario; just ignore it.
}
return documentation;
}
use of org.eclipse.jface.text.ITypedRegion in project linuxtools by eclipse.
the class STPCompletionProcessor method getProbe.
/**
* Returns the full name of the probe surrounding the given
* offset. This function assumes that the given offset is inside
* of a {@link STPPartitionScanner#STP_PROBE} section.
* @param document
* @param offset
* @return the probe name
* @throws BadLocationException
* @throws BadPartitioningException
*/
private String getProbe(IDocument document, int offset) throws BadLocationException, BadPartitioningException {
String probePoint = null;
ITypedRegion partition = ((IDocumentExtension3) document).getPartition(STPProbeScanner.STP_PROBE_PARTITIONING, offset, false);
String probe = document.get(partition.getOffset(), partition.getLength());
// make sure that we are inside a probe
if (probe.startsWith(PROBE_KEYWORD)) {
probePoint = probe.substring(PROBE_KEYWORD.length(), probe.indexOf('{'));
probePoint = probePoint.trim();
}
return probePoint;
}
use of org.eclipse.jface.text.ITypedRegion in project linuxtools by eclipse.
the class STPCompletionProcessor method computeCompletionProposals.
public ICompletionProposal[] computeCompletionProposals(IDocument document, int offset) {
ITypedRegion partition = null;
boolean useGlobal = false;
try {
partition = ((IDocumentExtension3) document).getPartition(STPProbeScanner.STP_PROBE_PARTITIONING, offset, false);
if (partition.getOffset() == offset) {
if (partition.getType() != IDocument.DEFAULT_CONTENT_TYPE && partition.getType() != STPProbeScanner.STP_PROBE) {
if (offset > 0) {
ITypedRegion prevPartition = ((IDocumentExtension3) document).getPartition(STPProbeScanner.STP_PROBE_PARTITIONING, offset - 1, false);
useGlobal = prevPartition.getType() == IDocument.DEFAULT_CONTENT_TYPE;
} else {
useGlobal = true;
}
}
}
} catch (BadLocationException | BadPartitioningException e) {
return NO_COMPLETIONS;
}
// $NON-NLS-1$
String prefix = "";
// $NON-NLS-1$
String prePrefix = "";
// Get completion hint from document
try {
prefix = getPrefix(document, offset);
Token previousToken = getPrecedingToken(document, offset - prefix.length() - 1);
while (// $NON-NLS-1$
previousToken.tokenString.equals("=") || previousToken.tokenString.equals(",")) {
// $NON-NLS-1$
previousToken = getPrecedingToken(document, previousToken.offset - 1);
previousToken = getPrecedingToken(document, previousToken.offset - 1);
}
prePrefix = previousToken.tokenString;
} catch (BadLocationException e) {
return NO_COMPLETIONS;
}
if (prePrefix.startsWith("probe")) {
// $NON-NLS-1$
return getProbeCompletionList(prefix, offset);
}
// which can be called.
if (partition.getType() == STPProbeScanner.STP_PROBE) {
ICompletionProposal[] variableCompletions = getProbeVariableCompletions(document, offset, prefix);
ICompletionProposal[] functionCompletions = getFunctionCompletions(offset, prefix);
ArrayList<ICompletionProposal> completions = new ArrayList<>(variableCompletions.length + functionCompletions.length);
completions.addAll(Arrays.asList(variableCompletions));
completions.addAll(Arrays.asList(functionCompletions));
return completions.toArray(new ICompletionProposal[0]);
} else if (partition.getType() == IDocument.DEFAULT_CONTENT_TYPE || useGlobal) {
// In the global scope return global keyword completion.
return getGlobalKeywordCompletion(prefix, offset);
}
return NO_COMPLETIONS;
}
Aggregations