From f3184ed63340892dc669a60f004c7d784f931135 Mon Sep 17 00:00:00 2001 From: Rune Date: Sun, 14 Jun 2026 18:17:24 +0200 Subject: [PATCH] virtual tokens between cursor --- src/Generator.bf | 55 ++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/Generator.bf b/src/Generator.bf index 3650d3a..064aaa3 100644 --- a/src/Generator.bf +++ b/src/Generator.bf @@ -580,29 +580,10 @@ abstract class Cpp2BeefGenerator bool first = true; for (let token in tokens) { - bool isWritingQueuedToken = false; - switch (Clang.GetTokenKind(token)) - { - case .Comment: - case .Punctuation: - mixin QueuedToken(var queuedToken, StringView spelling) - { - if (fileInfo.queuedTokens.HasFlag(queuedToken) && GetTokenSpelling!(token) == spelling) - { - fileInfo.queuedTokens ^= queuedToken; - isWritingQueuedToken = true; - } - } - - QueuedToken!(decltype(fileInfo.queuedTokens).LSquirly, "{"); - QueuedToken!(decltype(fileInfo.queuedTokens).RSquirly, "}"); - if (!fileInfo.queuedTokens.HasFlag(.RSquirly)) - QueuedToken!(decltype(fileInfo.queuedTokens).Semicolon, ";"); - - if (!isWritingQueuedToken) continue; - if (fileInfo.queuedTokens == .None) block = @block; - default: continue; - } + if (!FilterTokensBetweenCursors(token, let isWritingQueuedToken)) + continue; + if (isWritingQueuedToken && fileInfo.queuedTokens == .None) + block = @block; let location = Clang.GetTokenLocation(unit, token); Clang.GetFileLocation(location, ?, let startLine, ?, ?); WriteLinesUntil!(startLine, location); @@ -621,6 +602,34 @@ abstract class Cpp2BeefGenerator if (@block.HasValue) WriteBlock(@block.Value); } + protected virtual bool FilterTokensBetweenCursors(CXToken token, out bool isWritingQueuedToken) + { + isWritingQueuedToken = false; + switch (Clang.GetTokenKind(token)) + { + case .Comment: + case .Punctuation: + mixin QueuedToken(var queuedToken, StringView spelling) + { + if (fileInfo.queuedTokens.HasFlag(queuedToken) && GetTokenSpelling!(token) == spelling) + { + fileInfo.queuedTokens ^= queuedToken; + isWritingQueuedToken = true; + } + } + + QueuedToken!(decltype(fileInfo.queuedTokens).LSquirly, "{"); + QueuedToken!(decltype(fileInfo.queuedTokens).RSquirly, "}"); + if (!fileInfo.queuedTokens.HasFlag(.RSquirly)) + QueuedToken!(decltype(fileInfo.queuedTokens).Semicolon, ";"); + + if (!isWritingQueuedToken) return false; + default: + return false; + } + return true; + } + protected enum TypeParamMode { None, InParam, OutParam } protected virtual void Type(CXType type, TypeParamMode paramMode = .None) {