use of javax.swing.text.Element in project processing by processing.
the class LineID method startTracking.
/**
* Attach a {@link Document} to enable line number tracking when editing.
* The position to track is before the first non-whitespace character on the
* line. Edits happening before that position will cause the line number to
* update accordingly. Multiple {@link #startTracking} calls will replace
* the tracked document. Whoever wants a tracked line should track it and
* add itself as listener if necessary.
* ({@link LineHighlight}, {@link LineBreakpoint})
*
* @param doc the {@link Document} to use for line number tracking
*/
public synchronized void startTracking(Document doc) {
//System.out.println("tracking: " + this);
if (doc == null) {
// null arg
return;
}
if (doc == this.doc) {
// already tracking that doc
return;
}
try {
Element line = doc.getDefaultRootElement().getElement(lineIdx);
if (line == null) {
// line doesn't exist
return;
}
String lineText = doc.getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
// set tracking position at (=before) first non-white space character on line,
// or, if the line consists of entirely white spaces, just before the newline
// character
pos = doc.createPosition(line.getStartOffset() + nonWhiteSpaceOffset(lineText));
this.doc = doc;
doc.addDocumentListener(this);
} catch (BadLocationException ex) {
Messages.loge(null, ex);
pos = null;
this.doc = null;
}
}
use of javax.swing.text.Element in project jadx by skylot.
the class LineNumbers method getTextLineNumber.
protected String getTextLineNumber(int rowStartOffset) {
Element root = codeArea.getDocument().getDefaultRootElement();
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
if (line.getStartOffset() == rowStartOffset) {
int lineNumber = index + 1;
if (useSourceLines) {
Integer sourceLine = codeArea.getSourceLine(lineNumber);
if (sourceLine != null) {
return String.valueOf(sourceLine);
}
} else {
return String.valueOf(lineNumber);
}
}
return "";
}
use of javax.swing.text.Element in project jadx by skylot.
the class LineNumbers method isCurrentLine.
private boolean isCurrentLine(int rowStartOffset) {
int caretPosition = codeArea.getCaretPosition();
Element root = codeArea.getDocument().getDefaultRootElement();
return root.getElementIndex(rowStartOffset) == root.getElementIndex(caretPosition);
}
use of javax.swing.text.Element in project binnavi by google.
the class ConsoleHelpers method getWord.
/**
* Return all the characters forming a word (alpha-chars) from the given position backwards, up to
* the when the first delimiter is found.
*
* @param position location within the document from where to retrieve a word
*/
public String getWord(final int position) {
int offset = position;
final Element element = document.getParagraphElement(offset);
String elementText;
try {
elementText = document.getText(element.getStartOffset(), element.getEndOffset() - element.getStartOffset());
} catch (final Exception excp) {
return "";
}
final int elementTextLength = elementText.length();
if (elementTextLength == 0) {
return "";
}
int i = 0;
if (element.getStartOffset() > 0) {
offset = offset - element.getStartOffset();
}
for (i = offset - 1; i >= 0; i--) {
final char c = elementText.charAt(i);
if (document.isDelimiter("" + c) || (i == 0)) {
return elementText.substring(i + 1, offset).trim();
}
}
return "";
}
use of javax.swing.text.Element in project binnavi by google.
the class SyntaxDocument method getLine.
private String getLine(final String content, final int offset) {
final int line = rootElement.getElementIndex(offset);
final Element lineElement = rootElement.getElement(line);
final int start = lineElement.getStartOffset();
final int end = lineElement.getEndOffset();
return content.substring(start, end - 1);
}
Aggregations