TextMateLib 1.0
Modern C++ implementation of the TextMate syntax highlighting engine
Loading...
Searching...
No Matches
syntax_highlighter_c_api.h
1#ifndef TEXTMATELIB_SYNTAX_HIGHLIGHTER_C_API_H
2#define TEXTMATELIB_SYNTAX_HIGHLIGHTER_C_API_H
3
4#include "tml_export.h"
5#include "c_api.h"
6#include <stdint.h>
7#include <stddef.h>
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13// ============================================================================
14// Opaque Handle Types
15// ============================================================================
16
17typedef void* textmate_syntax_highlighter_t;
18typedef void* textmate_highlighted_token_t;
19typedef void* textmate_highlighted_line_t;
20
21// ============================================================================
22// C Structures Matching C++ Data
23// ============================================================================
24
25/// Corresponds to HighlightedToken in C++
26typedef struct {
27 int32_t startIndex;
28 int32_t endIndex;
29 const char** scopes;
30 int32_t scopeCount;
31
32 // Applied styling
33 const char* foregroundColor; // Hex color like "#FF0000"
34 const char* backgroundColor; // Hex color or NULL
35 int32_t fontStyle; // Bit flags: 1=italic, 2=bold, 4=underline, 8=strikethrough
36
37 // Metadata
38 int32_t tokenType; // StandardTokenType: 0=Other, 1=Comment, 2=String, 3=RegEx
39 const char* debugInfo;
41
42/// Corresponds to HighlightedLine in C++
43typedef struct {
44 int32_t lineIndex;
45 const char* content;
46 const textmate_highlighted_token_c* tokens;
47 int32_t tokenCount;
48 int32_t isComplete; // 0 = false, 1 = true
49 uint64_t version;
51
52/// Corresponds to SyntaxHighlightingMetadata in C++
53typedef struct {
54 uint64_t sessionId;
55 int32_t lineCount;
56 int32_t cachedLineCount;
57 double averageLineTokenizationMs;
58 int64_t lastUpdateMs;
59 const char* themeName;
60 int32_t themeColorCount;
62
63// ============================================================================
64// Lifecycle API
65// ============================================================================
66
67/// Create a new syntax highlighter
68/// @param grammar Grammar for tokenization (TextMateGrammar)
69/// @param theme Theme for styling (TextMateTheme)
70/// @return Opaque handle to highlighter, or NULL on error
71/// @note Caller must call textmate_syntax_highlighter_dispose() to free memory
72TML_API textmate_syntax_highlighter_t textmate_syntax_highlighter_create(
73 TextMateGrammar grammar,
74 TextMateTheme theme
75);
76
77/// Create a syntax highlighter with optional cache
78/// @param grammar Grammar for tokenization
79/// @param theme Theme for styling
80/// @param enableCache 1 to enable caching, 0 to disable
81/// @return Opaque handle to highlighter, or NULL on error
82TML_API textmate_syntax_highlighter_t textmate_syntax_highlighter_create_with_cache(
83 TextMateGrammar grammar,
84 TextMateTheme theme,
85 int32_t enableCache
86);
87
88/// Dispose of a syntax highlighter and free associated memory
89/// @param highlighter Highlighter to dispose
90TML_API void textmate_syntax_highlighter_dispose(textmate_syntax_highlighter_t highlighter);
91
92// ============================================================================
93// Document Management API
94// ============================================================================
95
96/// Set the entire document content
97/// @param highlighter Highlighter instance
98/// @param lines Array of line strings
99/// @param lineCount Number of lines
100TML_API void textmate_syntax_highlighter_set_document(
101 textmate_syntax_highlighter_t highlighter,
102 const char** lines,
103 int32_t lineCount
104);
105
106/// Edit a single line
107/// @param highlighter Highlighter instance
108/// @param lineIndex Line to edit (0-based)
109/// @param newContent New content for the line
110TML_API void textmate_syntax_highlighter_edit_line(
111 textmate_syntax_highlighter_t highlighter,
112 int32_t lineIndex,
113 const char* newContent
114);
115
116/// Insert lines at specified position
117/// @param highlighter Highlighter instance
118/// @param startIndex Position to insert (0-based)
119/// @param lines Array of lines to insert
120/// @param lineCount Number of lines to insert
121TML_API void textmate_syntax_highlighter_insert_lines(
122 textmate_syntax_highlighter_t highlighter,
123 int32_t startIndex,
124 const char** lines,
125 int32_t lineCount
126);
127
128/// Remove lines
129/// @param highlighter Highlighter instance
130/// @param startIndex Start of removal (0-based)
131/// @param count Number of lines to remove
132TML_API void textmate_syntax_highlighter_remove_lines(
133 textmate_syntax_highlighter_t highlighter,
134 int32_t startIndex,
135 int32_t count
136);
137
138/// Get current line count
139/// @param highlighter Highlighter instance
140/// @return Number of lines in document
141TML_API int32_t textmate_syntax_highlighter_get_line_count(
142 textmate_syntax_highlighter_t highlighter
143);
144
145// ============================================================================
146// Query API - Get Highlighted Content
147// ============================================================================
148
149/// Get syntax-highlighted version of a single line
150/// Returns fully resolved colors, fonts, and scope information
151/// @param highlighter Highlighter instance
152/// @param lineIndex Line to highlight (0-based)
153/// @return Opaque handle to HighlightedLine, or NULL on error
154/// @note Caller must call textmate_highlighted_line_dispose() to free memory
155TML_API textmate_highlighted_line_t textmate_syntax_highlighter_get_highlighted_line(
156 textmate_syntax_highlighter_t highlighter,
157 int32_t lineIndex
158);
159
160/// Get syntax-highlighted version of a line range (batch query)
161/// More efficient than calling get_highlighted_line multiple times
162/// @param highlighter Highlighter instance
163/// @param startIndex Start line (0-based, inclusive)
164/// @param endIndex End line (0-based, inclusive)
165/// @param outResults Array to store results (caller allocates)
166/// @param outResultCount Output: number of results returned
167/// @return Number of highlighted lines retrieved, or -1 on error
168TML_API int32_t textmate_syntax_highlighter_get_highlighted_range(
169 textmate_syntax_highlighter_t highlighter,
170 int32_t startIndex,
171 int32_t endIndex,
172 textmate_highlighted_line_t* outResults,
173 int32_t* outResultCount
174);
175
176/// Get raw tokens for a line (without theme styling)
177/// Useful for lower-level access or custom rendering
178/// @param highlighter Highlighter instance
179/// @param lineIndex Line to tokenize (0-based)
180/// @param outTokens Output array for tokens (caller allocates)
181/// @param outTokenCount Output: number of tokens
182/// @return Number of tokens, or -1 on error
183/// @note Token scopes are owned by the highlighter, do not free
184TML_API int32_t textmate_syntax_highlighter_get_line_tokens(
185 textmate_syntax_highlighter_t highlighter,
186 int32_t lineIndex,
187 TextMateToken* outTokens,
188 int32_t* outTokenCount
189);
190
191// ============================================================================
192// Theme Management API
193// ============================================================================
194
195/// Switch to a different theme
196/// Invalidates all cached highlighting
197/// @param highlighter Highlighter instance
198/// @param theme New theme to apply
199TML_API void textmate_syntax_highlighter_set_theme(
200 textmate_syntax_highlighter_t highlighter,
201 TextMateTheme theme
202);
203
204/// Get the currently active theme
205/// @param highlighter Highlighter instance
206/// @return Theme handle (do not dispose, owned by highlighter)
207TML_API TextMateTheme textmate_syntax_highlighter_get_theme(
208 textmate_syntax_highlighter_t highlighter
209);
210
211// ============================================================================
212// Cache Management API
213// ============================================================================
214
215/// Clear all cached highlighted lines
216/// Forces recomputation on next query
217/// @param highlighter Highlighter instance
218TML_API void textmate_syntax_highlighter_clear_cache(
219 textmate_syntax_highlighter_t highlighter
220);
221
222/// Invalidate cached highlighting for a line range
223/// @param highlighter Highlighter instance
224/// @param startIndex Start line (0-based)
225/// @param endIndex End line (0-based)
226TML_API void textmate_syntax_highlighter_invalidate_cache_range(
227 textmate_syntax_highlighter_t highlighter,
228 int32_t startIndex,
229 int32_t endIndex
230);
231
232// ============================================================================
233// Debugging & Monitoring API
234// ============================================================================
235
236/// Get debugging and performance metadata
237/// @param highlighter Highlighter instance
238/// @param outMetadata Output metadata structure (caller allocates)
239/// @return 0 on success, -1 on error
240TML_API int32_t textmate_syntax_highlighter_get_metadata(
241 textmate_syntax_highlighter_t highlighter,
243);
244
245// ============================================================================
246// Highlighted Line API (Accessors)
247// ============================================================================
248
249/// Get line index from highlighted line
250/// @param line Highlighted line handle
251/// @return Line index (0-based)
252TML_API int32_t textmate_highlighted_line_get_index(textmate_highlighted_line_t line);
253
254/// Get line content from highlighted line
255/// @param line Highlighted line handle
256/// @return Null-terminated string (owned by line, do not free)
257TML_API const char* textmate_highlighted_line_get_content(textmate_highlighted_line_t line);
258
259/// Get token count for highlighted line
260/// @param line Highlighted line handle
261/// @return Number of tokens
262TML_API int32_t textmate_highlighted_line_get_token_count(textmate_highlighted_line_t line);
263
264/// Get token at index from highlighted line
265/// @param line Highlighted line handle
266/// @param tokenIndex Token index (0-based)
267/// @return Token data (owned by line, do not free)
268TML_API const textmate_highlighted_token_c* textmate_highlighted_line_get_token(
269 textmate_highlighted_line_t line,
270 int32_t tokenIndex
271);
272
273/// Get completion status for highlighted line
274/// @param line Highlighted line handle
275/// @return 1 if tokenization completed, 0 if stopped early
276TML_API int32_t textmate_highlighted_line_is_complete(textmate_highlighted_line_t line);
277
278/// Get version of highlighted line
279/// @param line Highlighted line handle
280/// @return Version number for cache tracking
281TML_API uint64_t textmate_highlighted_line_get_version(textmate_highlighted_line_t line);
282
283/// Dispose of a highlighted line and free associated memory
284/// @param line Highlighted line to dispose
285TML_API void textmate_highlighted_line_dispose(textmate_highlighted_line_t line);
286
287// ============================================================================
288// Highlighted Token API (Accessors)
289// ============================================================================
290
291/// Get start index from token
292/// @param token Token handle
293/// @return Character position in line where token starts
294TML_API int32_t textmate_highlighted_token_get_start_index(textmate_highlighted_token_t token);
295
296/// Get end index from token
297/// @param token Token handle
298/// @return Character position in line where token ends
299TML_API int32_t textmate_highlighted_token_get_end_index(textmate_highlighted_token_t token);
300
301/// Get scope count for token
302/// @param token Token handle
303/// @return Number of scopes in the scope path
304TML_API int32_t textmate_highlighted_token_get_scope_count(textmate_highlighted_token_t token);
305
306/// Get scope at index from token
307/// @param token Token handle
308/// @param scopeIndex Scope index (0-based)
309/// @return Scope name (owned by token, do not free)
310TML_API const char* textmate_highlighted_token_get_scope(
311 textmate_highlighted_token_t token,
312 int32_t scopeIndex
313);
314
315/// Get foreground color from token
316/// @param token Token handle
317/// @return Hex color string like "#FF0000" or NULL if not set (owned by token, do not free)
318TML_API const char* textmate_highlighted_token_get_foreground_color(
319 textmate_highlighted_token_t token
320);
321
322/// Get background color from token
323/// @param token Token handle
324/// @return Hex color string or NULL if not set (owned by token, do not free)
325TML_API const char* textmate_highlighted_token_get_background_color(
326 textmate_highlighted_token_t token
327);
328
329/// Get font style from token
330/// @param token Token handle
331/// @return Bit flags: 1=italic, 2=bold, 4=underline, 8=strikethrough
332TML_API int32_t textmate_highlighted_token_get_font_style(textmate_highlighted_token_t token);
333
334/// Get token type from token
335/// @param token Token handle
336/// @return StandardTokenType: 0=Other, 1=Comment, 2=String, 3=RegEx
337TML_API int32_t textmate_highlighted_token_get_type(textmate_highlighted_token_t token);
338
339/// Get debug info from token
340/// @param token Token handle
341/// @return Debug string (owned by token, do not free)
342TML_API const char* textmate_highlighted_token_get_debug_info(textmate_highlighted_token_t token);
343
344/// Dispose of a highlighted token and free associated memory
345/// @param token Token to dispose
346TML_API void textmate_highlighted_token_dispose(textmate_highlighted_token_t token);
347
348#ifdef __cplusplus
349}
350#endif
351
352#endif // TEXTMATELIB_SYNTAX_HIGHLIGHTER_C_API_H
C language API for TextMateLib.
void * TextMateGrammar
Handle to a grammar definition for a specific language.
Definition c_api.h:38
void * TextMateTheme
Handle to a theme object containing color schemes.
Definition c_api.h:35
Represents a single token in tokenized text.
Definition c_api.h:58
Corresponds to HighlightedLine in C++.
Corresponds to HighlightedToken in C++.
Corresponds to SyntaxHighlightingMetadata in C++.