public int getListOffset(RSyntaxTextArea textArea, TabExpander e, float x0, float x) { // If the coordinate in question is before this line's start, quit. if (x0 >= x) return getOffset(); float currX = x0; // x-coordinate of current char. float nextX = x0; // x-coordinate of next char. float stableX = x0; // Cached ending x-coord. of last tab or token. TokenImpl token = this; int last = getOffset(); FontMetrics fm = null; while (token != null && token.isPaintable()) { fm = textArea.getFontMetricsForTokenType(token.getType()); char[] text = token.text; int start = token.textOffset; int end = start + token.textCount; for (int i = start; i < end; i++) { currX = nextX; if (text[i] == '\t') { nextX = e.nextTabStop(nextX, 0); stableX = nextX; // Cache ending x-coord. of tab. start = i + 1; // Do charsWidth() from next char. } else { nextX = stableX + fm.charsWidth(text, start, i - start + 1); } if (x >= currX && x < nextX) { if ((x - currX) < (nextX - x)) { return last + i - token.textOffset; } return last + i + 1 - token.textOffset; } } stableX = nextX; // Cache ending x-coordinate of token. last += token.textCount; token = (TokenImpl) token.getNextToken(); } // If we didn't find anything, return the end position of the text. return last; }
public Rectangle listOffsetToView( RSyntaxTextArea textArea, TabExpander e, int pos, int x0, Rectangle rect) { int stableX = x0; // Cached ending x-coord. of last tab or token. TokenImpl token = this; FontMetrics fm = null; Segment s = new Segment(); while (token != null && token.isPaintable()) { fm = textArea.getFontMetricsForTokenType(token.getType()); if (fm == null) { return rect; // Don't return null as things'll error. } char[] text = token.text; int start = token.textOffset; int end = start + token.textCount; // If this token contains the position for which to get the // bounding box... if (token.containsPosition(pos)) { s.array = token.text; s.offset = token.textOffset; s.count = pos - token.getOffset(); // Must use this (actually fm.charWidth()), and not // fm.charsWidth() for returned value to match up with where // text is actually painted on OS X! int w = Utilities.getTabbedTextWidth(s, fm, stableX, e, token.getOffset()); rect.x = stableX + w; end = token.documentToToken(pos); if (text[end] == '\t') { rect.width = fm.charWidth(' '); } else { rect.width = fm.charWidth(text[end]); } return rect; } // If this token does not contain the position for which to get // the bounding box... else { s.array = token.text; s.offset = token.textOffset; s.count = token.textCount; stableX += Utilities.getTabbedTextWidth(s, fm, stableX, e, token.getOffset()); } token = (TokenImpl) token.getNextToken(); } // If we didn't find anything, we're at the end of the line. Return // a width of 1 (so selection highlights don't extend way past line's // text). A ConfigurableCaret will know to paint itself with a larger // width. rect.x = stableX; rect.width = 1; return rect; }