Skip to content

Fix variable member autocomplete - #4

Merged
shakeelmohamed merged 3 commits into
mainfrom
dev
Sep 2, 2026
Merged

shakeelmohamed merged 3 commits into
mainfrom
dev

Conversation

@shakeelmohamed

@shakeelmohamed shakeelmohamed commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

Summary by Sourcery

Make GLSL variable autocomplete scope-aware so identifiers resolve to the correct visible declaration and provide accurate completion options.

New Features:

  • Offer in-scope user variables in word completion and resolve their types for member and index completions.
  • Provide member completions for supported built-in GLSL variables such as gl_FragCoord.

Bug Fixes:

  • Prevent same-name variables in other functions from incorrectly shadowing the variable at the cursor.
  • Exclude out-of-scope and not-yet-declared variables from autocomplete suggestions and type resolution.

Enhancements:

  • Replace global variable scanning with cached, scope-aware analysis of function parameters, locals, and file-scope declarations.
  • Expose development-only editor cursor and document helpers for completion testing.

Tests:

  • Expand unit and end-to-end coverage for scope-aware variable resolution, declaration visibility, one-line functions, built-in member completion, and completion prioritization.

@sourcery-ai

sourcery-ai Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Autocomplete now discovers GLSL variables by file and enclosing-function scope, so member resolution honors local shadowing and word completion suggests only visible user variables ahead of built-ins. Unit and end-to-end tests cover the new scope behavior, with development-only editor hooks supporting reliable cursor placement in live documents; generated docs assets are updated accordingly.

Sequence diagram for scope-aware GLSL variable autocomplete

sequenceDiagram
    participant Editor as CodeMirror Editor
    participant Completion as glslCompletionSource
    participant Scope as enclosingFunction
    participant Variables as variablesInScope
    participant Resolver as resolveVariable

    Editor->>Completion: request completion
    Completion->>Scope: enclosingFunction(doc, ctx.pos)
    Scope-->>Completion: current function scope
    Completion->>Variables: variablesInScope(doc, scope)
    Variables-->>Completion: visible variables and types
    Completion-->>Editor: variable options before built-ins

    Editor->>Completion: request member completion
    Completion->>Scope: enclosingFunction(doc, ctx.pos)
    Scope-->>Completion: current function scope
    Completion->>Resolver: resolveVariable(doc, scope, name)
    Resolver-->>Completion: effective variable type
    Completion-->>Editor: membersForType(type)
Loading

File-Level Changes

Change Details Files
Make variable discovery and type resolution function-scope aware.
  • Track declarations with their enclosing function scope while scanning GLSL source.
  • Resolve file-scope and same-function declarations with local shadowing and exclude other functions.
  • Add helpers for determining the enclosing function and listing visible variables.
src/glsl-vars.ts
Use scoped variables for member and word autocomplete.
  • Resolve dot/bracket member types in the cursor’s enclosing function.
  • Offer visible user variables in word completion with higher priority than built-ins.
  • Preserve existing member, keyword, type, and constructor completion behavior.
src/glsl-completions.ts
Add coverage for scope-aware autocomplete behavior.
  • Test declaration tracking, shadowing, scope resolution, and visible-variable enumeration.
  • Add end-to-end tests for same-name variables across functions and variable completion ranking/exclusion.
  • Compute test cursor offsets from the live auto-indented document.
test/glsl-vars.test.ts
test/autocomplete.e2e.ts
Expose development-only editor inspection and cursor controls.
  • Add browser hooks to set the CodeMirror cursor and read the live document for end-to-end tests.
  • Declare the new window APIs in global TypeScript types.
src/editor.ts
src/global.d.ts
Update generated documentation assets to reference the rebuilt bundle.
  • Replace the hashed JavaScript asset reference in the documentation HTML.
  • Regenerate the bundled documentation JavaScript output.
docs/index.html
docs/assets/index-B0C_gFvc.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/glsl-vars.ts" line_range="61" />
<code_context>
+
+// Optional qualifier span and precision, then <type> <name> [array] [= init];
+const DECL = /^(?:(?:const|uniform|in|out|varying|attribute)\s+)?(?:(?:highp|mediump|lowp)\s+)?(\w+)\s+(\w+)(?:\s*\[[^\]]*\])?(?:\s*=\s*[^;]*?)?\s*;?$/;
+// A function definition — return type, name, params, opening brace on one line.
+const FUNC_HEADER = /^([\w.]+)\s+(\w+)\s*\([^)]*\)\s*\{\s*$/;
+
</code_context>
<issue_to_address>
**issue (bug_risk):** Function definitions whose opening brace is on the following line do not match `FUNC_HEADER`, so their declarations are recorded at file scope. Same-name locals in such functions then leak across function boundaries and member autocomplete resolves the wrong variable type.

**Triggers:** When GLSL is formatted as `void main()` followed by a separate `{` line.

**Suggested fix:** Track a function header across lines or recognize the opening brace independently of the header line.
</issue_to_address>

### Comment 2
<location path="src/glsl-vars.ts" line_range="96-101" />
<code_context>
+      if (fm) {
+        // A function definition opens a new scope for the rest of its body.
+        depth += braces(line);
+        stack.push({ name: fm[2], baseDepth: depth });
+        scope = fm[2];
+      } else {
+        depth += braces(line);
+        while (stack.length && stack[stack.length - 1].baseDepth > depth) stack.pop();
+        scope = stack.length ? stack[stack.length - 1].name : "";
+      }
+    }
</code_context>
<issue_to_address>
**issue (bug_risk):** An empty one-line function such as `void main() {}` has a brace delta of zero, so its stack entry gets `baseDepth: 0` and is never removed by the `baseDepth > depth` check. The parser therefore treats every subsequent declaration as belonging to that function instead of file scope.

**Triggers:** When the source contains an empty one-line function before later declarations or functions.

**Suggested fix:** Pop function scopes when the function's closing brace is already on its header line, or track whether the function is still open separately from brace depth.
</issue_to_address>

### Comment 3
<location path="src/glsl-vars.ts" line_range="60-132" />
<code_context>
+const DECL = /^(?:(?:const|uniform|in|out|varying|attribute)\s+)?(?:(?:highp|mediump|lowp)\s+)?(\w+)\s+(\w+)(?:\s*\[[^\]]*\])?(?:\s*=\s*[^;]*?)?\s*;?$/;
</code_context>
<issue_to_address>
**issue (bug_risk):** Function parameters are not matched by `DECL` and are never added to `scanVariables`, so a parameter such as `vec3 normal` cannot be resolved and `normal.` produces no member completions. Parameters are also absent from word completion even though they are variables in the enclosing function scope.

**Triggers:** When the identifier before `.` or `[` is a function parameter.

**Suggested fix:** Parse function parameters and add each parameter to the corresponding function scope before resolving or listing variables.
</issue_to_address>

### Comment 4
<location path="src/glsl-vars.ts" line_range="145-150" />
<code_context>
+ * in the same function shadows it. Out-of-scope declarations (other functions)
+ * are never consulted. Among the visible candidates the later one wins.
+ */
+export function resolveVariable(src: string, scope: string, name: string): string | null {
+  const decls = scanVariables(src).get(name);
+  if (!decls) return null;
+  let type: string | null = null;
+  for (const d of decls) {
+    if (d.scope === scope || d.scope === "") type = d.type;
+  }
+  return type;
</code_context>
<issue_to_address>
**issue (bug_risk):** `resolveVariable` and `variablesInScope` consider every declaration in the matching function scope, regardless of its position relative to the cursor, and the last matching declaration wins. Before a local declaration, autocomplete therefore resolves or offers a variable that is not yet visible in GLSL source order.

**Triggers:** When completion is requested before a same-function declaration with the referenced name.

**Suggested fix:** Pass the cursor position into resolution and filter declarations to those occurring before that position before applying shadowing.
</issue_to_address>

Sourcery assessment

Approval pending. 4 findings to address first.

Blocking findings: src/glsl-vars.ts:61, src/glsl-vars.ts:101, src/glsl-vars.ts:132, src/glsl-vars.ts:150


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/glsl-vars.ts Outdated
Comment thread src/glsl-vars.ts Outdated
Comment thread src/glsl-vars.ts Outdated
Comment thread src/glsl-vars.ts Outdated
Comment on lines +145 to +150
export function resolveVariable(src: string, scope: string, name: string): string | null {
const decls = scanVariables(src).get(name);
if (!decls) return null;
let type: string | null = null;
for (const d of decls) {
if (d.scope === scope || d.scope === "") type = d.type;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): resolveVariable and variablesInScope consider every declaration in the matching function scope, regardless of its position relative to the cursor, and the last matching declaration wins. Before a local declaration, autocomplete therefore resolves or offers a variable that is not yet visible in GLSL source order.

Triggers: When completion is requested before a same-function declaration with the referenced name.

Suggested fix: Pass the cursor position into resolution and filter declarations to those occurring before that position before applying shadowing.

@shakeelmohamed
shakeelmohamed merged commit 5d5139a into main Sep 2, 2026
3 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.

1 participant