vscode-tlaplus icon indicating copy to clipboard operation
vscode-tlaplus copied to clipboard

Feature/update vars command

Open younes-io opened this issue 5 months ago • 11 comments

This PR implements the update vars feature requested in #364.

  • TLA+: Update vars tuple command that syncs your vars tuple with VARIABLES declarations

image

  • Handles single/multi-line VARIABLES, multiple declarations, and VARIABLE/VARIABLES keywords
  • Preserves your multi-line formatting style (2, 3, 4+ items per line)
  • Auto-converts long single-line vars to multi-line for readability
  • Maintains indentation patterns
  • Lightbulb 💡 appears when cursor is on vars == line

image

  • One-click update without opening command palette
  • (kind of) PlusCal support : Detects --algorithm and --fair algorithm
  • New setting: tlaplus.refactor.includePlusCalVariables (default: true)

image

  • Handles pc and stack variables based on the user's preference (VS Code settings)

https://github.com/user-attachments/assets/18bbc295-3777-4596-99ad-9dfa98699823

I use this file for manual tests (in debug mode):

---- MODULE TestModule ----

VARIABLES aleph, beh, teh, theh, jeem, hah, khah, dal, thal, ra, zay, seen
VARIABLES sheen, sad, dad, tah, zah, ain, ghain, fa, qaf, kaf, lam, meem, noon, ha, waw, ya
VARIABLE single

vars == <<>>

Init == x = 0 /\ y = 0 /\ z = 0 /\ t = 5
Next == x' = x + 1 /\ y' = y + 1 /\ z' = z + 1 /\ t' = t + 2
Spec == Init /\ [][Next]_vars

====

Note: This PR does NOT implement all the expectations :)

younes-io avatar Jun 07 '25 09:06 younes-io

for PlusCal (very basic for now):

https://github.com/user-attachments/assets/75f16fc4-feb5-44ac-b365-d821a9065476

younes-io avatar Jun 07 '25 15:06 younes-io

@hwayne You requested this feature—could you take a look and help with the review?

lemmy avatar Jun 08 '25 23:06 lemmy

I would like to abandon this PR and replace it with a new clean one (& use the tree-sitter). It will be cleaner and more reliable, I think.

Your thoughts? @lemmy @ahelwer

younes-io avatar Jun 13 '25 14:06 younes-io

I'm certainly happy to support the VS Code extension's use of tree-sitter-tlaplus from the side of developing tree-sitter-tlaplus, so file issues if there are any features you need.

ahelwer avatar Jun 13 '25 14:06 ahelwer

I would like to abandon this PR and replace it with a new clean one (& use the tree-sitter). It will be cleaner and more reliable, I think.

Your thoughts? @lemmy @ahelwer

I have reservations about adding yet another parser to the VSCode extension. There are already subtle inconsistencies in parse results between SANY (the standard parser) and the TLAPS parser when TLAPS is activated.

lemmy avatar Jun 13 '25 15:06 lemmy

in this case, I keep this PR as-is for now, sticking to the existing code pattern: hybrid approach: SANY/XMLExport, and regex as fallback.

(I just thought that tree-sitter could be an efficient replacement for these regex & custom parsing algos, for reliability, TLA+ syntax coverage, etc. Of course, we would still use SANY just as we do today for semantics)

younes-io avatar Jun 13 '25 15:06 younes-io

https://github.com/user-attachments/assets/1c19e3b0-3c0c-4f9b-a1c3-af75072230ec

younes-io avatar Jun 13 '25 15:06 younes-io

I have reservations about adding yet another parser to the VSCode extension. There are already subtle inconsistencies in parse results between SANY (the standard parser) and the TLAPS parser when TLAPS is activated.

I would argue the inconsistencies between regexes and SANY are far, far larger than any inconsistency between tree-sitter and SANY, since both conform almost entirely to the syntactic test corpus.

ahelwer avatar Jun 13 '25 15:06 ahelwer

I would argue the inconsistencies between regexes and SANY are far, far larger than any inconsistency between tree-sitter and SANY, since both conform almost entirely to the syntactic test corpus.

Replacing all regular expressions (syntax highlighting) with tree-sitter sounds like a good idea to me. We should use SANY wherever it's capable of handling the job (recovery mode should be assumed to occur eventually in SANY).

lemmy avatar Jun 13 '25 16:06 lemmy

Architecturally, will tree-sitter work in the VSCode browser mode on e.g. github.dev?

lemmy avatar Jun 13 '25 16:06 lemmy

I don't know how to check that but in terms of toolchain requirements tree-sitter-tlaplus is a large generated C file. The NPM package contains pre-built binaries of tree-sitter-tlaplus for x64 and arm64, so users of the package don't need to have a C compiler installed. tree-sitter-tlaplus can be compiled to webassembly (which is what Spectacle uses) but that has a different API than the NPM package I think.

ahelwer avatar Jun 13 '25 16:06 ahelwer

Any thoughts on this one?

younes-io avatar Jul 19 '25 14:07 younes-io

tree-sitter-tlaplus can be compiled to webassembly (which is what Spectacle uses) but that has a different API than the NPM package I think.

@ahelwer Are there any disadvantages to using the WebAssembly API?

lemmy avatar Jul 21 '25 15:07 lemmy

Not that I know of, although I have never used it personally. You're thinking you would run webassembly even locally?

ahelwer avatar Jul 21 '25 15:07 ahelwer

@ahelwer Are there any disadvantages to using the WebAssembly API?

@ahelwer Are there any disadvantages to using WebAssembly locally?

lemmy avatar Jul 21 '25 15:07 lemmy

Hmm, also have never tried, but my understanding is you would need to bundle a WebAssembly runtime engine (idk how big that is) and it would almost definitely be slower than a natively-compiled tree-sitter grammar. Probably would still be fine for usability? Also don't know what TypeScript -> WebAssembly interop looks like.

ahelwer avatar Jul 21 '25 15:07 ahelwer

It seems to me that tree-sitter-tlaplus brings quite a bit of overhead. I don’t think we want to end up with an extension that’s either platform-specific or significantly larger than its current size of under 10 MB.

lemmy avatar Jul 21 '25 15:07 lemmy

VS Code certainly supports platform-specific extensions where you can conditionally bundle native pre-compiled build artifacts for the specific target platform. A compiled tree-sitter-tlaplus grammar is currently around 4.7 MB - LR(1) parser tables can get quite large for ambiguous languages like TLA+!

If you want to be able to support both native and web-based execution of the extension (do the bundled Java tools work in that scenario?) you'll likely need to use the webassembly version, which uses different API bindings as far as I know.

ahelwer avatar Jul 21 '25 15:07 ahelwer

closing this until the parser architecture question is resolved

younes-io avatar Aug 15 '25 18:08 younes-io

@younes-io are you able to join the monthly community calls? It is a good way to push & resolve issues which are not getting attention. You are a great developer and it would be a shame to lose you due to stalled PRs!

ahelwer avatar Aug 15 '25 18:08 ahelwer

@ahelwer Thank you for the kind words! I'd be happy to join the community call on Sept. I also understand the architectural considerations are important to get right

younes-io avatar Aug 15 '25 18:08 younes-io