TextMateLib 1.0
Modern C++ implementation of the TextMate syntax highlighting engine
Loading...
Searching...
No Matches
session.h
1#ifndef TEXTMATELIB_SESSION_H
2#define TEXTMATELIB_SESSION_H
3
4#include "grammar.h"
5#include "types.h"
6#include <vector>
7#include <memory>
8#include <cstdint>
9#include <string>
10#include <map>
11
12namespace tml {
13
14// Forward declaration
15class SessionImpl;
16
17// Represents a cached line with tokens and state
18struct SessionLine {
19 std::string content; // Line content
20 std::vector<IToken> tokens; // Cached tokens
21 StateStack* state; // State at end of line
22 uint64_t version; // Version for change tracking
23 bool cached; // Whether this line has been tokenized
24
25 SessionLine()
26 : content(""),
27 state(nullptr),
28 version(0),
29 cached(false) {}
30
31 ~SessionLine();
32};
33
34// Session metadata for debugging and monitoring
35struct SessionMetadata {
36 uint64_t createdAtMs; // Timestamp of creation
37 uint32_t referenceCount; // Current reference count
38 int32_t lineCount; // Current line count
39 int32_t cachedLineCount; // Number of cached lines
40 uint64_t memoryUsageBytes; // Approximate memory usage
41};
42
43// High-level stateful tokenization interface for text editors
44//
45// The Session API provides incremental tokenization with automatic state management.
46// It caches tokens and state per line, allowing editors to efficiently handle
47// incremental edits with early stopping when state stabilizes.
48class SessionImpl {
49private:
50 // Session identity and state
51 uint64_t sessionId; // Unique session identifier
52 std::shared_ptr<IGrammar> grammar; // Grammar for tokenization
53 std::vector<SessionLine> lines; // Cached lines with tokens and state
54 uint32_t referenceCount; // Reference count for memory management
55 uint64_t createdAtMs; // Creation time in milliseconds
56 uint64_t lastAccessMs; // Last access time for expiry
57 uint64_t nextVersion; // Version counter for cache invalidation
58
59 // Incremental tokenization helpers
60 bool isStateEqual(StateStack* state1, StateStack* state2) const;
61 void invalidateFrom(int32_t startIndex);
62 void retokenizeLines(int32_t startIndex, int32_t endIndex);
63
64public:
65 // Lifecycle
66 SessionImpl(uint64_t id, std::shared_ptr<IGrammar> gram);
67 ~SessionImpl();
68
69 // Reference counting
70 void retain();
71 void release();
72 uint32_t getRefCount() const { return referenceCount; }
73
74 // Queries
75 uint64_t getSessionId() const { return sessionId; }
76 uint64_t getCreatedAtMs() const { return createdAtMs; }
77 uint64_t getLastAccessMs() const { return lastAccessMs; }
78 bool isExpired(uint64_t currentTimeMs, uint64_t maxAgeMs) const;
79
80 // State management
81 int32_t setLines(const std::vector<std::string>& newLines);
82 int32_t getLineCount() const { return static_cast<int32_t>(lines.size()); }
83
84 // Incremental tokenization operations
85 int32_t edit(
86 const std::vector<std::string>& newLines,
87 int32_t startIndex,
88 int32_t replaceCount
89 );
90
91 int32_t add(
92 const std::vector<std::string>& newLines,
93 int32_t insertIndex
94 );
95
96 int32_t remove(
97 int32_t startIndex,
98 int32_t removeCount
99 );
100
101 // Query operations
102 const SessionLine* getLine(int32_t lineIndex) const;
103 const std::vector<IToken>* getLineTokens(int32_t lineIndex) const;
104 StateStack* getLineState(int32_t lineIndex) const;
105
106 // Batch query
107 void getTokensRange(
108 int32_t startIndex,
109 int32_t endIndex,
110 std::vector<SessionLine>& results
111 ) const;
112
113 // Maintenance operations
114 void invalidateRange(int32_t startIndex, int32_t endIndex);
115 void clearCache();
116 SessionMetadata getMetadata() const;
117
118private:
119 void updateAccessTime();
120 uint64_t calculateMemoryUsage() const;
121 int32_t countCachedLines() const;
122};
123
124// Global session manager with reference counting
125class SessionManager {
126private:
127 static std::map<uint64_t, std::shared_ptr<SessionImpl>> sessions;
128 static uint64_t nextSessionId;
129 static uint32_t operationCount;
130 static const uint32_t CLEANUP_INTERVAL = 100;
131
132public:
133 // Session lifecycle
134 static uint64_t createSession(std::shared_ptr<IGrammar> grammar);
135 static std::shared_ptr<SessionImpl> getSession(uint64_t sessionId);
136 static void retainSession(uint64_t sessionId);
137 static void releaseSession(uint64_t sessionId);
138 static void disposeSession(uint64_t sessionId);
139
140 // Maintenance
141 static void cleanupExpired(int32_t maxAgeMs);
142 static void triggerPeriodicCleanup(int32_t maxAgeMs);
143
144 // Debug/monitoring
145 static size_t getSessionCount();
146};
147
148} // namespace tml
149
150#endif // TEXTMATELIB_SESSION_H
Core type definitions and interfaces for TextMateLib.