001package org.clafer.lsp; 002 003import java.util.ArrayList; 004import java.util.List; 005import org.clafer.frontend.SourceSpan; 006import org.eclipse.lsp4j.Position; 007import org.eclipse.lsp4j.Range; 008 009/** 010 * Converts between the frontend's positions and LSP's, on the text of a 011 * document. 012 * <p> 013 * The frontend's lines and columns start at 1, and its columns are the 014 * compiler's: a tab moves to the column after the next multiple of 8, and 015 * every other character, a carriage return included, is one column. LSP's 016 * lines and characters start at 0, and a character is a UTF-16 code unit, as 017 * a Java {@code char} is. 018 */ 019public final class Positions { 020 021 private final List<String> lines; 022 023 /** 024 * @param text the text of the document 025 */ 026 public Positions(String text) { 027 List<String> ls = new ArrayList<>(); 028 int start = 0; 029 for (int i = 0; i < text.length(); i++) { 030 if (text.charAt(i) == '\n') { 031 ls.add(text.substring(start, i)); 032 start = i + 1; 033 } 034 } 035 ls.add(text.substring(start)); 036 this.lines = List.copyOf(ls); 037 } 038 039 /** 040 * @param line a line, from 0 041 * @return its text without the newline, or empty if there is no such line 042 */ 043 public String line(int line) { 044 return line >= 0 && line < lines.size() ? lines.get(line) : ""; 045 } 046 047 /** 048 * @return the number of lines, the last one after the last newline 049 * included 050 */ 051 public int lineCount() { 052 return lines.size(); 053 } 054 055 /** 056 * @param line a line of the frontend, from 1 057 * @param column a column of the frontend, from 1 058 * @return the LSP position: past the end of its line if the column is, 059 * and at the end of the document if the line is past it 060 */ 061 public Position toLsp(int line, int column) { 062 if (line > lines.size()) { 063 int last = lines.size() - 1; 064 return new Position(last, lines.get(last).length()); 065 } 066 String text = lines.get(Math.max(line, 1) - 1); 067 int col = 1; 068 for (int i = 0; i < text.length(); i++) { 069 int after = next(col, text.charAt(i)); 070 // a column inside a tab is the tab 071 if (col >= column || after > column) { 072 return new Position(line - 1, i); 073 } 074 col = after; 075 } 076 return new Position(Math.max(line, 1) - 1, text.length()); 077 } 078 079 /** 080 * @param position an LSP position 081 * @return the frontend's line and column there, as {line, column} 082 */ 083 public int[] fromLsp(Position position) { 084 String text = line(position.getLine()); 085 int col = 1; 086 for (int i = 0; i < Math.min(position.getCharacter(), text.length()); i++) { 087 col = next(col, text.charAt(i)); 088 } 089 return new int[]{position.getLine() + 1, col}; 090 } 091 092 /** 093 * The column after a character, as the compiler counts it. 094 */ 095 private static int next(int column, char c) { 096 return c == '\t' ? ((column + 7) / 8) * 8 + 1 : column + 1; 097 } 098 099 /** 100 * @param span a span of the frontend 101 * @return its LSP range 102 */ 103 public Range toLsp(SourceSpan span) { 104 return new Range(toLsp(span.line(), span.column()), toLsp(span.endLine(), span.endColumn())); 105 } 106 107 /** 108 * @param span a span of the frontend 109 * @return its LSP range, widened to the character at its start if it is 110 * empty and there is one, so that every editor shows it 111 */ 112 public Range toVisibleLsp(SourceSpan span) { 113 Range r = toLsp(span); 114 if (r.getStart().equals(r.getEnd())) { 115 Position s = r.getStart(); 116 if (s.getCharacter() < line(s.getLine()).length()) { 117 return new Range(s, new Position(s.getLine(), s.getCharacter() + 1)); 118 } 119 } 120 return r; 121 } 122 123 /** 124 * @param range a range in this document 125 * @return its text 126 */ 127 public String text(Range range) { 128 Position s = range.getStart(), e = range.getEnd(); 129 if (s.getLine() == e.getLine()) { 130 String l = line(s.getLine()); 131 return l.substring(Math.min(s.getCharacter(), l.length()), Math.min(e.getCharacter(), l.length())); 132 } 133 StringBuilder sb = new StringBuilder(); 134 String first = line(s.getLine()); 135 sb.append(first.substring(Math.min(s.getCharacter(), first.length()))); 136 for (int i = s.getLine() + 1; i < e.getLine(); i++) { 137 sb.append('\n').append(line(i)); 138 } 139 String last = line(e.getLine()); 140 sb.append('\n').append(last, 0, Math.min(e.getCharacter(), last.length())); 141 return sb.toString(); 142 } 143}