Skip to content

Commit 3d6ac2c

Browse files
Copilothotlong
andcommitted
Fix code review issues: use substring instead of substr, improve context detection
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 3b8c38e commit 3d6ac2c

2 files changed

Lines changed: 42 additions & 10 deletions

File tree

packages/vscode-extension/src/providers/CompletionProvider.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class CompletionProvider
1414
): vscode.ProviderResult<vscode.CompletionItem[] | vscode.CompletionList> {
1515
const linePrefix = document
1616
.lineAt(position)
17-
.text.substr(0, position.character);
17+
.text.substring(0, position.character);
1818

1919
// Get completion items based on context
2020
const items: vscode.CompletionItem[] = [];
@@ -148,17 +148,32 @@ export class CompletionProvider
148148

149149
/**
150150
* Get current type from document context
151+
* Note: This is a simplified implementation. For production use,
152+
* consider using a proper JSON parser or AST for accurate context detection.
151153
*/
152154
private getCurrentType(
153155
document: vscode.TextDocument,
154156
position: vscode.Position
155157
): string | null {
156-
// Simple implementation - in production would use proper JSON parsing
157-
const text = document.getText();
158-
const offset = document.offsetAt(position);
159-
const before = text.substring(Math.max(0, offset - 200), offset);
160-
161-
const match = before.match(/"type"\s*:\s*"(\w+)"/);
162-
return match ? match[1] : null;
158+
try {
159+
const text = document.getText();
160+
const offset = document.offsetAt(position);
161+
162+
// Look backwards for the nearest "type" property
163+
const before = text.substring(Math.max(0, offset - 500), offset);
164+
165+
// Find all type declarations in the context
166+
const typeMatches = Array.from(before.matchAll(/"type"\s*:\s*"(\w+)"/g));
167+
168+
// Return the most recent one
169+
if (typeMatches.length > 0) {
170+
return typeMatches[typeMatches.length - 1][1];
171+
}
172+
} catch (error) {
173+
// Fail silently - just won't provide context-specific completions
174+
console.error('Error detecting current type:', error);
175+
}
176+
177+
return null;
163178
}
164179
}

packages/vscode-extension/src/providers/SchemaValidator.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,31 @@ export class SchemaValidator {
183183

184184
/**
185185
* Find the range of a property in the document
186+
* Note: This is a simplified implementation that returns a default range.
187+
* TODO: For production use, implement proper JSON parsing to find exact property locations.
188+
* Consider using a JSON parser with position tracking or VSCode's built-in JSON language service.
186189
*/
187190
private findPropertyRange(
188191
document: vscode.TextDocument,
189192
path: string,
190193
property: string
191194
): vscode.Range {
192-
// For now, return the start of the document
193-
// In a full implementation, this would parse the JSON and find the exact location
195+
try {
196+
// Attempt to find the property in the document
197+
const text = document.getText();
198+
const searchPattern = new RegExp(`"${property}"\\s*:`);
199+
const match = searchPattern.exec(text);
200+
201+
if (match && match.index !== undefined) {
202+
const pos = document.positionAt(match.index);
203+
return new vscode.Range(pos, pos.translate(0, property.length + 2));
204+
}
205+
} catch (error) {
206+
// If we can't find it, fall back to the beginning
207+
console.error('Error finding property range:', error);
208+
}
209+
210+
// Fallback: return the start of the document
194211
return new vscode.Range(0, 0, 0, 1);
195212
}
196213

0 commit comments

Comments
 (0)