Skip to content

Optimize Semantic Token Language Server Request#1865

Merged
anderseknert merged 2 commits intoopen-policy-agent:mainfrom
SeanLedford:s_ledford/optimize-semantic-tokens-request
Feb 11, 2026
Merged

Optimize Semantic Token Language Server Request#1865
anderseknert merged 2 commits intoopen-policy-agent:mainfrom
SeanLedford:s_ledford/optimize-semantic-tokens-request

Conversation

@SeanLedford
Copy link
Contributor

These changes are meant to optimize the request for semantic tokens between the client and the language server. Instead of preparing the module required to parse the textDocument prior to every request, the module is now loaded from the regal bundle.

@SeanLedford SeanLedford force-pushed the s_ledford/optimize-semantic-tokens-request branch 2 times, most recently from 138b0bc to 72fe28c Compare February 11, 2026 18:50
Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Some minor things to fix, then good to merge 👍

# description: Extract function argument declarations
arg_tokens.declaration contains var if {
some rule in module.rules
some var in rule.head.args
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an advanced topic sure, but args aren't necessarily vars! If they aren't they'll be pattern matched for equality in a call. So for example this definition:

f("foo", y) if {
    y > 100
}

Would be the same as:

f(x, y) if {
    x == "foo"
    y > 100
}

Here's one example of where we use this in Regal to describe an "or switch" pretty much.

So while it's not too common, you'll want to ensure the type is var here:

arg_tokens.declaration contains arg if {
    some rule in module.rules
    some arg in rule.head.args
    arg.type == "var"
}

Copy link
Contributor Author

@SeanLedford SeanLedford Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah gotcha, addressed! Went ahead and changed the arg_token naming to var_token as well to be more exact. On second thought, we're still checking for function args only here, just checking that they're used as variables. So I'll leave it as is

]
}
}
} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just complaining about not having a last newline in the file.

some var in call.args
# description: Extract variable references in function calls
arg_tokens.reference contains var if {
some rule in module.rules
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add count(rule.head.args) > 0 below or else you'll be running this on all rules (not just functions)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good idea, added.

var.type == "var"

arg_names := {v.value | some v in ast.found.vars[rule_index].args}
arg_names := {v.value | some v in rule.head.args}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to do the same check for type == "var" here.

Also, this will be the same for all vars in the rule, so this should be moved up above the walk

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed the location of the function arg name check. I did already have a type check for the arg I was pulling from the array of terms, so just want to verify that's what you meant.

var.type == "var"

arg_names := {v.value | some v in ast.found.vars[rule_index].args}
arg_names := {v.value | some v in rule.head.args}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above applies here.

@SeanLedford SeanLedford force-pushed the s_ledford/optimize-semantic-tokens-request branch 2 times, most recently from 860a23a to 96ce194 Compare February 11, 2026 21:33
… query from bundle

Signed-off-by: Sean Ledford <s_ledford@apple.com>
Signed-off-by: Sean Ledford <s_ledford@apple.com>
@SeanLedford SeanLedford force-pushed the s_ledford/optimize-semantic-tokens-request branch from 96ce194 to 1de6fa1 Compare February 11, 2026 21:34
Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@anderseknert anderseknert merged commit 743444c into open-policy-agent:main Feb 11, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants