Skip to content

chore: Simplify extension#25

Merged
attitude merged 3 commits into
mainfrom
chore/simplify-extension
Apr 12, 2026
Merged

chore: Simplify extension#25
attitude merged 3 commits into
mainfrom
chore/simplify-extension

Conversation

@attitude
Copy link
Copy Markdown
Owner

@attitude attitude commented Apr 12, 2026

Summary

Simplification (chore: Simplify extension)

  • providers.ts: Extracted delegateLocationProvider() helper shared by PHPXDefinitionProvider, PHPXTypeDefinitionProvider, and PHPXImplementationProvider — eliminated ~60 lines of triplicated boilerplate. Simplified mapLocationLinkToSource using a single remapUri variable instead of three mutable lets. Removed dead PHPXWorkspaceSymbolProvider class (exported but never registered in extension.ts).
  • compiler.ts: Moved stderrOverflow declaration above its handler (was declared one line after use). Added stderrOverflow to the close handler's overflow branch — previously only truncated was checked, leaving stderr-overflow kills to fall through to code !== 0 and attempt JSON.parse on a truncated buffer. Removed dead compilationErrors map and getCompilationError() method (written but never read externally). Fixed err.message || err.stack order in compileAndWrite catch — stack now used for logging, message for user-facing error.
  • diagnostics.ts: Removed shouldForwardDiagnostic() stub (always returned true) and its no-op .filter() call site.
  • positionMapper.ts: Removed /g flag from module-level PHP_WORD_PATTERNdocument.getWordRangeAtPosition() throws if passed a global regex (this was silently swallowing the error in all three location providers). Fixed double-statSync on cache miss: stat first, then read.
  • languageClient.ts: Removed redundant ?? undefined from optional-chain expression.
  • extension.ts: Deduplicated the three identical vendorWatcher event handlers into a shared clearCompilerCache callback.

Bug fix (fix(positionMapper): use fresh regex in getWordAt for matchAll compatibility)

  • getWordAt uses String.matchAll() which requires a global regex, but PHP_WORD_PATTERN no longer has /g. Fixed by creating a fresh new RegExp(PHP_WORD_PATTERN.source, 'g') at the call site — consistent with the pattern already used in getNthOccurrence and findNthOccurrence.

Tests (test(extension): fix false positives and strengthen assertions)

  • extension.test.ts: Replaced circular languageId test (was forcibly setting language: 'phpx' then asserting languageId === 'phpx') with a real auto-detection test that opens a temp .phpx file by URI.
  • grammar.test.ts: Removed runGrammarTests() — it looped over 12 test cases with expectedScopes arrays that were never read; every case unconditionally incremented passed. Actual tokenization is covered by pnpm test:grammar via vscode-tmgrammar-test. Strengthened validateGrammarStructure to assert non-empty patterns and repository.
  • range-remap.test.ts: Fixed executeCommand mocks to capture and assert the forwarded PHP URI and PHPX→PHP mapped column — the forward position mapping was previously completely untested. Added end-position assertions to all four tests (only start was previously checked). Added phpxTitleStart !== phpTitleStart guard to guarantee the fixture requires non-trivial column remapping.

Test plan

  • TypeScript compilation passes (pnpm compile — no errors)
  • All 8 integration tests pass (pnpm test)
  • Grammar tests pass (pnpm test:grammar)
  • Go-to-definition, type definition, and find implementation work in .phpx files
  • stderr overflow during compilation produces "Compilation output exceeded size limit" instead of a JSON parse error
  • No regression in diagnostics forwarding from PHP language server to .phpx files

🤖 Generated with Claude Code

@attitude attitude mentioned this pull request Apr 12, 2026
4 tasks
@attitude attitude force-pushed the chore/simplify-extension branch from 7738cd7 to 7322e83 Compare April 12, 2026 21:22
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Simplifies the PHPX VS Code extension by removing dead code, deduplicating provider boilerplate, and tightening up compiler overflow handling and position/diagnostic remapping behaviors.

Changes:

  • Refactors definition/type-definition/implementation providers to share a single delegation helper and simplifies LocationLink remapping.
  • Improves compiler process overflow handling and removes unused compilation error tracking.
  • Cleans up diagnostics forwarding and strengthens/remodels several extension-host tests.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
extension/src/providers.ts Deduplicates location-style providers via a shared delegate helper; simplifies LocationLink remapping logic; removes unused workspace symbol provider.
extension/src/compiler.ts Fixes stderr overflow handling in close path; removes unused compilation error map; improves error logging/message selection.
extension/src/diagnostics.ts Removes no-op diagnostic filter and forwards diagnostics directly with remapped ranges.
extension/src/positionMapper.ts Removes /g from shared word regex constant; adjusts cache miss file read/stat ordering; updates matchAll usage to use fresh regex instances.
extension/src/languageClient.ts Removes redundant ?? undefined from cwd derivation.
extension/src/extension.ts Deduplicates vendor watcher cache-invalidation handlers.
extension/src/test/range-remap.test.ts Strengthens range/position remap assertions and validates forwarded URI/position for rename/prepareRename.
extension/src/test/grammar.test.ts Replaces placeholder “runner” logic with a focused Mocha structure-validation test and clearer documentation.
extension/src/test/extension.test.ts Simplifies activation test and adds a file-extension-based language auto-detection test.
Comments suppressed due to low confidence (2)

extension/src/compiler.ts:190

  • The output-limit checks compare stdout.length (string code units) to maxOutputBytes (bytes). This can under/over-count when output contains non-ASCII and makes the truncation threshold inaccurate. Track byte counts separately (e.g., stdoutBytes += data.length) and compare that to maxOutputBytes, or accumulate buffers and use total Buffer length.
			proc.stdout!.on('data', (data: Buffer) => {
				try {
					if (stdout.length < maxOutputBytes) {
						stdout += data.toString();
					} else if (!truncated) {
						truncated = true;
						this.outputChannel.appendLine(`[PHPX] stdout truncated — exceeded ${maxOutputBytes} bytes (buffer: ${stdout.length}, chunk: ${data.length})`);
						proc.kill();
					}

extension/src/compiler.ts:206

  • Same byte-vs-string-length issue for stderr: stderr.length is not bytes, so the overflow guard may not enforce the intended maxOutputBytes limit. Consider maintaining a stderrBytes counter based on data.length and using that for comparisons/logging.
			proc.stderr!.on('data', (data: Buffer) => {
				try {
					if (stderr.length < maxOutputBytes) {
						stderr += data.toString();
					} else if (!stderrOverflow) {
						stderrOverflow = true;
						this.outputChannel.appendLine(`[PHPX] stderr truncated — exceeded ${maxOutputBytes} bytes (buffer: ${stderr.length}, chunk: ${data.length})`);
						proc.kill();
					}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@attitude attitude marked this pull request as ready for review April 12, 2026 21:53
@attitude attitude merged commit 4ad26f6 into main Apr 12, 2026
7 checks passed
@attitude attitude deleted the chore/simplify-extension branch April 12, 2026 21:53
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