6 | * A query are what treesitter uses to extract informations from the syntax tree. 7 | *
8 | *9 | * see How to write query? 10 | *
11 | */ 12 | public interface TSLangaugeQuery { 13 | String highlight(); 14 | 15 | String indent(); 16 | } 17 | -------------------------------------------------------------------------------- /editor/src/main/java/io/ikws4/codeeditor/language/TSLanguageStyler.java: -------------------------------------------------------------------------------- 1 | package io.ikws4.codeeditor.language; 2 | 3 | import androidx.annotation.Nullable; 4 | 5 | import java.util.ArrayList; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | 9 | import io.ikws4.codeeditor.api.configuration.SyntaxColorScheme; 10 | import io.ikws4.codeeditor.api.document.markup.Markup; 11 | import io.ikws4.codeeditor.api.language.LanguageStyler; 12 | import io.ikws4.jsitter.TSNode; 13 | import io.ikws4.jsitter.TSParser; 14 | import io.ikws4.jsitter.TSQuery; 15 | import io.ikws4.jsitter.TSQueryCapture; 16 | import io.ikws4.jsitter.TSTree; 17 | 18 | public abstract class TSLanguageStyler implements LanguageStyler { 19 | private static final String TAG = "TSLanguageStyler"; 20 | 21 | private final static HashMap58 | * You need to copy a syntax tree in order to use it on more than 59 | * one thred at a time, as syntax trees are not thread safe. 60 | * 61 | * @param treePtr tree pointer 62 | * @return copyed tree pointer 63 | */ 64 | static native long treeCopy(long treePtr); 65 | 66 | /** 67 | * Delete the syntax tree, freeing all of the memory that it used. 68 | * 69 | * @param treePtr tree pointer 70 | */ 71 | static native void treeDelete(long treePtr); 72 | 73 | /** 74 | * Get the root node of the syntax tree. 75 | * 76 | * @param treePtr tree pointer 77 | * @return {@link TSNode} 78 | */ 79 | static native TSNode treeRootNode(long treePtr); 80 | 81 | /** 82 | * Get the langauge that was used to parse the syntax tree. 83 | * 84 | * @param treePtr tree pointer 85 | * @return langauge pointer see {@link TSLanguages} 86 | */ 87 | static native long treeLanguage(long treePtr); 88 | 89 | /** 90 | * Edit the syntax tree to keep it in sync with source code that 91 | * has been edited. 92 | *
93 | * You mut describe the edit both in terms of byte offsets and in terms 94 | * of (row, column) coordinates. 95 | * 96 | *
97 | * typedef struct { 98 | * uint32_t start_byte; 99 | * uint32_t old_end_byte; 100 | * uint32_t new_end_byte; 101 | * TSPoint start_point; 102 | * TSPoint old_end_point; 103 | * TSPoint new_end_point; 104 | * } TSInputEdit; 105 | * 106 | * typedef struct { 107 | * uint32_t row; 108 | * uint32_t column; 109 | * } TSPoint; 110 | *111 | * 112 | * @param treePtr tree pointer 113 | */ 114 | static native void treeEdit(long treePtr, 115 | int startByte, 116 | int oldEndByte, 117 | int newEndByte, 118 | int startRow, int startColumn, 119 | int oldEndRow, int oldEndColumn, 120 | int newEndRow, int newEndColumn); 121 | 122 | 123 | //************************************************************************* 124 | //* Section - TSNode 125 | //************************************************************************* 126 | 127 | /** 128 | * Get the node's immediate parent. 129 | */ 130 | @Nullable 131 | static native TSNode nodeParant(long id, long tree_ptr, int context0, int context1, int context2, int context3); 132 | 133 | /** 134 | * Get the node's child at the given index, where zero represents the first 135 | * child. 136 | */ 137 | @Nullable 138 | static native TSNode nodeChild(long id, long tree_ptr, int context0, int context1, int context2, int context3, int index); 139 | 140 | /** 141 | * get the node's number of children. 142 | */ 143 | static native int nodeChildCount(long id, long tree_ptr, int context0, int context1, int context2, int context3); 144 | 145 | /** 146 | * Get the node's named child at the given index, where zero represents 147 | * the first child. 148 | */ 149 | @Nullable 150 | static native TSNode nodeNamedChild(long id, long tree_ptr, int context0, int context1, int context2, int context3, int index); 151 | 152 | /** 153 | * get the node's number of children. 154 | */ 155 | static native int nodeNamedChildCount(long id, long tree_ptr, int context0, int context1, int context2, int context3); 156 | 157 | /** 158 | * Get the node's type as a null-terminated string. 159 | */ 160 | static native String nodeType(long id, long tree_ptr, int context0, int context1, int context2, int context3); 161 | 162 | /** 163 | * Get the node's type as a numberial id. 164 | */ 165 | static native int nodeSymbol(long id, long tree_ptr, int context0, int context1, int context2, int context3); 166 | 167 | /** 168 | * Check is the node is named Named nodes correspond to named rules in the 169 | * grammar, whereas anonymous nodes correspond to string literals in the grammar. 170 | */ 171 | static native boolean nodeIsNamed(long id, long tree_ptr, int context0, int context1, int context2, int context3); 172 | 173 | /** 174 | * Check if the node is missing. Missing nodes are inserted by the parser in 175 | * order to recover from certain kinds of syntax errors. 176 | */ 177 | static native boolean nodeIsMissing(long id, long tree_ptr, int context0, int context1, int context2, int context3); 178 | 179 | /** 180 | * Check if the node is syntax error or contains any syntax errors. 181 | */ 182 | static native boolean nodeHasError(long id, long tree_ptr, int context0, int context1, int context2, int context3); 183 | 184 | /** 185 | * Get an S-expression representing the node as a string. 186 | */ 187 | static native String nodeString(long id, long tree_ptr, int context0, int context1, int context2, int context3); 188 | 189 | /** 190 | * Get the smallest node within this node that spans the given range of (row, column) positions. 191 | */ 192 | @Nullable 193 | static native TSNode nodeDescendantForRange(long id, long tree_ptr, int context0, int context1, int context2, int context3, int startRow, int startColumn, int endRow, int endColumn); 194 | 195 | /** 196 | * Get the smallest named node within this node that spans the given range of (row, column) positions. 197 | */ 198 | @Nullable 199 | static native TSNode nodeNamedDescendantForRange(long id, long tree_ptr, int context0, int context1, int context2, int context3, int startRow, int startColumn, int endRow, int endColumn); 200 | 201 | 202 | //************************************************************************* 203 | //* Section - TreeCursor 204 | //************************************************************************* 205 | 206 | /** 207 | * Create a new tree cursor starting from the given node. 208 | *
209 | * A tree cursor allows you to walk a syntax tree more efficiently 210 | * than is possible using the {@link TSNode} functions. It is a mutable 211 | * object that is always on a certain syntax node, and can be moved 212 | * imperatively to different nodes. 213 | * 214 | * @return TSTreeCursor pointer 215 | */ 216 | static native long treeCursorNew(long id, long tree_ptr, int context0, int context1, int context2, int context3); 217 | 218 | /** 219 | * Delete a tree cursor, freeing all of the memory that it used. 220 | * 221 | * @param treeCursorPtr TSTreeCursor pointer 222 | */ 223 | static native void treeCursorDelete(long treeCursorPtr); 224 | 225 | /** 226 | * Get the tree cursor's current node. 227 | * 228 | * @param treeCursorPtr TSTreeCursor pointer 229 | * @return Current tree node 230 | */ 231 | static native TSNode treeCursorCurrentNode(long treeCursorPtr); 232 | 233 | /** 234 | * Move the cursor to the parent of its current node. 235 | * 236 | * @param treeCursorPtr TSTreeCursor pointer 237 | * @return true if the cursor successfully moved, 238 | * false if there was no parent node. 239 | */ 240 | static native boolean treeCursorGotoParent(long treeCursorPtr); 241 | 242 | /** 243 | * Move the cursor to the next sibling of its current node. 244 | * 245 | * @param treeCursorPtr TSTreeCursor pointer 246 | * @return true if the cursor successfully moved, 247 | * false if there was no the sibling node. 248 | */ 249 | static native boolean treeCursorGotoNextSibling(long treeCursorPtr); 250 | 251 | /** 252 | * Move the cursor to the first child of its current ndoe. 253 | * 254 | * @param treeCursorPtr TSTreeCursor painter 255 | * @return true if the cursor successfully moved, 256 | * false if there were no children. 257 | */ 258 | static native boolean treeCursorGotoFirstChild(long treeCursorPtr); 259 | 260 | 261 | //************************************************************************* 262 | //* Section - Query 263 | //************************************************************************* 264 | 265 | /** 266 | * Create a new query from a string containing one or more S-expression 267 | * patterns. The query is associated with particular langauge, and can 268 | * only be run on syntax nodes parsed with that langauge. 269 | *
270 | * If all of the given patterns are valid, this return a {@link TSQuery} pointer. 271 | * If a pattern is invalid, this return 0 (NULL) 272 | * 273 | * @param languagePtr see {@link TSLanguages} 274 | * @param source S-expression 275 | * @param length S-expression length 276 | * @return TSQuery pointer 277 | */ 278 | static native long queryNew(long languagePtr, String source, int length); 279 | 280 | /** 281 | * Delete a query, freeing all of the memory that is used. 282 | * 283 | * @param queryPtr TSQuery pointer 284 | */ 285 | static native void queryDelete(long queryPtr); 286 | 287 | /** 288 | * Create a new cursor for executing a given query. 289 | *
290 | * The cursor stores the state that is needed to iteratively search 291 | * for matches. To use the query cursor, first call {@link #queryCursorExec(long, long, long, long, int, int, int, int)} 292 | * to start running a given query on a given syntax node. 293 | * 294 | * @return TSQueryCursor pointer 295 | */ 296 | static native long queryCursorNew(); 297 | 298 | /** 299 | * Delete a query cursor, freeing all of the memory that is used. 300 | * 301 | * @param queryCursorPtr TSQueryCursor pointer 302 | */ 303 | static native void queryCursorDelete(long queryCursorPtr); 304 | 305 | /** 306 | * Start running a give query on a given {@link TSNode}. 307 | * 308 | * @param queryCursorPtr TSQueryCursor pointer 309 | * @param queryPtr TSQuery pointer 310 | */ 311 | static native void queryCursorExec(long queryCursorPtr, long queryPtr, long id, long treePtr, int context0, int context1, int context2, int context3); 312 | 313 | /** 314 | * Set the range of bytes in whitch the query will be executed. 315 | * @param queryCursorPtr TSQueryCursor pointer 316 | */ 317 | static native void queryCursorSetByteRange(long queryCursorPtr, int startByte, int endByte); 318 | 319 | /** 320 | * Set the range of (row, column) in whitch the query will be executed. 321 | * @param queryCursorPtr TSQueryCursor pointer 322 | */ 323 | static native void queryCursorSetPointRange(long queryCursorPtr, int startRow, int startColumn, int endRow, int endColumn); 324 | 325 | /** 326 | * Adcance to the next match of the currently running query. 327 | * 328 | * @param queryCursorPtr TSQueryCursor pointer 329 | * @param queryPtr TSQuery pointer 330 | * @return {@link TSQueryMatch} if there is a match. Otherwise, return null. 331 | */ 332 | @Nullable 333 | static native TSQueryMatch queryCursorNextMatch(long queryCursorPtr, long queryPtr); 334 | } -------------------------------------------------------------------------------- /screenshot/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/CodeEditor/ec300715612665fa8dc9d3ba8dd4f7416679c3f7/screenshot/1.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':jsitter' 2 | include ':editor' 3 | include ':app' 4 | rootProject.name = "CodeEditor" --------------------------------------------------------------------------------