Skip to content

Commit d71874c

Browse files
committed
Revert "ref: handle definition request"
This reverts commit 8285443.
1 parent 8285443 commit d71874c

File tree

2 files changed

+57
-122
lines changed

2 files changed

+57
-122
lines changed

schemas/shared/block.schema.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"title": "Block's id",
4+
"description": "Block's id",
45
"type": "string"
5-
}
6+
}

server/src/handleDefinitionRequest.ts

+55-121
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,77 @@
1-
import { DefinitionParams, LocationLink } from 'vscode-languageserver';
1+
import { readFileSync } from 'fs';
2+
import { DefinitionParams } from 'vscode-languageserver';
23
import { URI } from 'vscode-uri';
3-
import * as JSONC from 'jsonc-parser';
44
import { BlocksHashMap } from './BlocksHashMap';
55
import { getBlockPositionInText } from './utils/getBlockPositionInText';
66

7-
/**
8-
* Handles a request to provide the definition of a block reference at a given text document position.
9-
* This allows users to navigate to the definition of a block by clicking on its reference.
10-
*/
11-
export const handleDefinitionRequest = (params: DefinitionParams, blocksHashMap: BlocksHashMap): LocationLink[] | undefined => {
7+
// Handles a request to provide the definition of a symbol at a given text document position.
8+
export const handleDefinitionRequest = (params: DefinitionParams, blocksHashMap: BlocksHashMap) => {
129
const { textDocument, position } = params;
1310

1411
const filePath = URI.parse(textDocument.uri).fsPath;
1512
const fileText = blocksHashMap.getFileContent(filePath);
16-
if (!fileText) return undefined;
1713

18-
// Convert position to offset for JSONC scanner
19-
const offset = getOffsetAtPosition(fileText, position);
14+
const lines = fileText.split(/\r?\n/);
15+
const line = lines[position.line];
2016

21-
// Find the string token at the current position
22-
const stringToken = findStringTokenAtOffset(fileText, offset);
23-
if (!stringToken) return undefined;
17+
if (!line) return undefined;
2418

25-
const { value: blockId, range } = stringToken;
26-
console.log('blockId', blockId);
27-
console.log('range', range);
19+
const hoveredChar = line.charAt(position.character);
2820

29-
// Check if this is a block reference (not a property name)
30-
if (isPropertyName(fileText, range.start)) return undefined;
21+
let initialChar = hoveredChar;
22+
let endChar = hoveredChar;
23+
let matchStartsAt = position.character;
24+
let matchEndsAt = position.character + 1;
3125

32-
// Try to find the definition of the block
33-
try {
34-
const definitionFilePath = blocksHashMap.getBlockFilePath(blockId);
35-
if (!definitionFilePath) return undefined;
26+
while (initialChar !== '"' || endChar !== '"') {
27+
if (initialChar !== '"') matchStartsAt--;
28+
if (endChar !== '"') matchEndsAt++;
3629

37-
const definitionFileContent = blocksHashMap.getFileContent(definitionFilePath);
38-
if (!definitionFileContent) return undefined;
30+
if (matchStartsAt < 0) return undefined;
31+
if (matchEndsAt > line.length) return undefined;
3932

40-
const definitionPosition = getBlockPositionInText(blockId, definitionFileContent);
41-
if (!definitionPosition) return undefined;
42-
43-
// Convert the token range to document positions for proper highlighting
44-
const originRange = {
45-
start: positionAt(fileText, range.start + 1), // +1 to skip the opening quote
46-
end: positionAt(fileText, range.end - 1) // -1 to skip the closing quote
47-
};
48-
49-
// Return a LocationLink which includes originSelectionRange for proper highlighting
50-
return [{
51-
originSelectionRange: originRange,
52-
targetUri: URI.file(definitionFilePath).toString(),
53-
targetRange: definitionPosition,
54-
targetSelectionRange: definitionPosition
55-
}];
56-
} catch (error) {
57-
console.error(`Error finding definition for block "${blockId}":`, error);
58-
return undefined;
33+
initialChar = line?.charAt(matchStartsAt);
34+
endChar = line?.charAt(matchEndsAt);
5935
}
60-
};
6136

62-
/**
63-
* Calculates the character offset at a given position in the document
64-
*/
65-
function getOffsetAtPosition(text: string, position: { line: number; character: number }): number {
66-
const lines = text.split(/\r?\n/);
67-
let offset = 0;
37+
matchStartsAt++;
6838

69-
for (let i = 0; i < position.line; i++) {
70-
offset += lines[i].length + 1; // +1 for the newline character
71-
}
39+
const match = line.substring(matchStartsAt, matchEndsAt);
40+
const matchIsADefinition = line.includes(`"${match}":`);
7241

73-
return offset + position.character;
74-
}
75-
76-
/**
77-
* Converts an offset to a Position object
78-
*/
79-
function positionAt(text: string, offset: number): { line: number; character: number } {
80-
const lines = text.split(/\r?\n/);
81-
let currentOffset = 0;
82-
let lineNumber = 0;
83-
84-
for (const line of lines) {
85-
const lineLength = line.length + 1; // +1 for the newline character
86-
if (currentOffset + lineLength > offset) {
87-
return {
88-
line: lineNumber,
89-
character: offset - currentOffset
90-
};
91-
}
92-
currentOffset += lineLength;
93-
lineNumber++;
94-
}
42+
if (matchIsADefinition) return undefined;
9543

96-
// Fallback for end of document
97-
return {
98-
line: lines.length - 1,
99-
character: lines[lines.length - 1].length
100-
};
101-
}
102-
103-
/**
104-
* Finds a string token at the given offset in the text
105-
*/
106-
function findStringTokenAtOffset(text: string, offset: number): { value: string; range: { start: number; end: number } } | undefined {
107-
const scanner = JSONC.createScanner(text, true);
108-
let token = scanner.scan();
109-
110-
while (token !== JSONC.SyntaxKind.EOF) {
111-
if (token === JSONC.SyntaxKind.StringLiteral) {
112-
const start = scanner.getTokenOffset();
113-
const length = scanner.getTokenLength();
114-
const end = start + length;
115-
116-
// Check if cursor is inside this string token
117-
if (offset >= start && offset <= end) {
118-
return {
119-
value: scanner.getTokenValue(),
120-
range: { start, end }
121-
};
122-
}
123-
}
124-
token = scanner.scan();
125-
}
126-
127-
return undefined;
128-
}
129-
130-
/**
131-
* Checks if a string at the given position is a property name (key) in JSON
132-
*/
133-
function isPropertyName(text: string, position: number): boolean {
134-
const scanner = JSONC.createScanner(text, true);
135-
scanner.setPosition(position);
44+
try {
45+
const definitionFilePath = blocksHashMap.getBlockFilePath(match);
46+
const definitionFileContent = blocksHashMap.getFileContent(definitionFilePath);
47+
const definitionPosition = getBlockPositionInText(match, definitionFileContent);
13648

137-
// Scan the string token
138-
scanner.scan();
49+
if (!definitionPosition) return undefined;
13950

140-
// Check if the next token is a colon
141-
const nextToken = scanner.scan();
142-
return nextToken === JSONC.SyntaxKind.ColonToken;
143-
}
51+
const {
52+
start: { line: startLine, character: startCharacter },
53+
end: { line: endLine, character: endCharacter }
54+
} = definitionPosition;
55+
56+
return [
57+
{
58+
uri: textDocument.uri,
59+
originSelectionRange: {
60+
start: { line: position.line, character: matchStartsAt },
61+
end: { line: position.line, character: matchEndsAt }
62+
},
63+
targetUri: URI.file(definitionFilePath).toString(),
64+
targetSelectionRange: {
65+
start: { line: startLine, character: startCharacter },
66+
end: { line: endLine, character: endCharacter }
67+
},
68+
targetRange: {
69+
start: { line: startLine, character: startCharacter },
70+
end: { line: endLine, character: endCharacter }
71+
}
72+
}
73+
];
74+
} catch (error) {
75+
return;
76+
}
77+
};

0 commit comments

Comments
 (0)