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 // Reclaims the StateStack / AttributedScopeStack node graph backing this session's
60 // cached per-line states. Retokenization installs this arena, then sweeps it down to
61 // the nodes still reachable from the currently-cached line states — freeing intra-line
62 // scan garbage and states superseded by edits. Without it these nodes leak, since the
63 // cache only ever overwrites the `state` pointer (see SessionLine::~SessionLine).
64 StackNodeArena stateArena;
65
66 // Incremental tokenization helpers
67 bool isStateEqual(StateStack* state1, StateStack* state2) const;
68 void invalidateFrom(int32_t startIndex);
69 void retokenizeLines(int32_t startIndex, int32_t endIndex);
70
71public:
72 // Lifecycle
73 SessionImpl(uint64_t id, std::shared_ptr<IGrammar> gram);
74 ~SessionImpl();
75
76 // Reference counting
77 void retain();
78 void release();
79 uint32_t getRefCount() const { return referenceCount; }
80
81 // Queries
82 uint64_t getSessionId() const { return sessionId; }
83 uint64_t getCreatedAtMs() const { return createdAtMs; }
84 uint64_t getLastAccessMs() const { return lastAccessMs; }
85 bool isExpired(uint64_t currentTimeMs, uint64_t maxAgeMs) const;
86
87 // State management
88 int32_t setLines(const std::vector<std::string>& newLines);
89 int32_t getLineCount() const { return static_cast<int32_t>(lines.size()); }
90
91 // Incremental tokenization operations
92 int32_t edit(
93 const std::vector<std::string>& newLines,
94 int32_t startIndex,
95 int32_t replaceCount
96 );
97
98 int32_t add(
99 const std::vector<std::string>& newLines,
100 int32_t insertIndex
101 );
102
103 int32_t remove(
104 int32_t startIndex,
105 int32_t removeCount
106 );
107
108 // Query operations
109 const SessionLine* getLine(int32_t lineIndex) const;
110 const std::vector<IToken>* getLineTokens(int32_t lineIndex) const;
111 StateStack* getLineState(int32_t lineIndex) const;
112
113 // Batch query
114 void getTokensRange(
115 int32_t startIndex,
116 int32_t endIndex,
117 std::vector<SessionLine>& results
118 ) const;
119
120 // Maintenance operations
121 void invalidateRange(int32_t startIndex, int32_t endIndex);
122 void clearCache();
123 SessionMetadata getMetadata() const;
124
125private:
126 void updateAccessTime();
127 uint64_t calculateMemoryUsage() const;
128 int32_t countCachedLines() const;
129};
130
131// Global session manager with reference counting
132class SessionManager {
133private:
134 static std::map<uint64_t, std::shared_ptr<SessionImpl>> sessions;
135 static uint64_t nextSessionId;
136 static uint32_t operationCount;
137 static const uint32_t CLEANUP_INTERVAL = 100;
138
139public:
140 // Session lifecycle
141 static uint64_t createSession(std::shared_ptr<IGrammar> grammar);
142 static std::shared_ptr<SessionImpl> getSession(uint64_t sessionId);
143 static void retainSession(uint64_t sessionId);
144 static void releaseSession(uint64_t sessionId);
145 static void disposeSession(uint64_t sessionId);
146
147 // Maintenance
148 static void cleanupExpired(int32_t maxAgeMs);
149 static void triggerPeriodicCleanup(int32_t maxAgeMs);
150
151 // Debug/monitoring
152 static size_t getSessionCount();
153};
154
155} // namespace tml
156
157#endif // TEXTMATELIB_SESSION_H
Core type definitions and interfaces for TextMateLib.