Skip to content

Commit cb014d2

Browse files
committed
feat(LanguageServiceProvider): refactored comment detection
Signed-off-by: Kishan Patel <[email protected]>
1 parent c577ad2 commit cb014d2

File tree

2 files changed

+63
-54
lines changed

2 files changed

+63
-54
lines changed

server/src/sas/LanguageServiceProvider.ts

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -216,62 +216,17 @@ export class LanguageServiceProvider {
216216
}
217217

218218
private addCommentFolding(result: FoldingRange[]) {
219-
const lineCount = this.model.getLineCount();
219+
// Get all comment ranges from the lexer
220+
const commentRanges = this.syntaxProvider.getAllCommentRanges();
220221

221-
// Method 1: Try token blocks (works for comments inside code blocks)
222-
if (this.syntaxProvider.lexer.tknBlks) {
223-
const tokenBlocks = this.syntaxProvider.lexer.tknBlks;
224-
for (let i = 0; i < tokenBlocks.length; i++) {
225-
const block = tokenBlocks[i];
226-
const isComment =
227-
block.blockComment === true || block.type === "comment";
228-
if (isComment && block.endLine > block.startLine) {
229-
result.push({
230-
startLine: block.startLine,
231-
endLine: block.endLine,
232-
kind: "comment",
233-
});
234-
}
235-
}
222+
for (const range of commentRanges) {
223+
result.push({
224+
startLine: range.startLine,
225+
endLine: range.endLine,
226+
kind: "comment",
227+
});
236228
}
237-
238-
// Method 2: Scan for multiline comments using syntax tokens (for standalone comments)
239-
let inBlockComment = false;
240-
let commentStartLine = -1;
241-
242-
for (let lineNum = 0; lineNum < lineCount; lineNum++) {
243-
const line = this.model.getLine(lineNum);
244-
245-
if (!inBlockComment && line.includes("/*")) {
246-
const commentStart = line.indexOf("/*");
247-
const commentEnd = line.indexOf("*/", commentStart + 2);
248-
249-
if (commentEnd === -1) {
250-
inBlockComment = true;
251-
commentStartLine = lineNum;
252-
}
253-
} else if (inBlockComment && line.includes("*/")) {
254-
inBlockComment = false;
255-
if (commentStartLine !== -1 && lineNum > commentStartLine) {
256-
const isAlreadyCovered = result.some(
257-
(range) =>
258-
range.startLine === commentStartLine && range.endLine === lineNum,
259-
);
260-
261-
if (!isAlreadyCovered) {
262-
result.push({
263-
startLine: commentStartLine,
264-
endLine: lineNum,
265-
kind: "comment",
266-
});
267-
}
268-
}
269-
commentStartLine = -1;
270-
}
271-
}
272-
}
273-
274-
// DFS
229+
} // DFS
275230
private _flattenFoldingBlockTree(rootBlock: FoldingBlock): FoldingBlock[] {
276231
const stack: FoldingBlock[] = [rootBlock];
277232
const resultList: FoldingBlock[] = [];

server/src/sas/SyntaxProvider.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,58 @@ export class SyntaxProvider {
423423
}
424424
return block.name;
425425
}
426+
getTokenBlocks(): any[] {
427+
return this.lexer.tknBlks || [];
428+
}
429+
getAllCommentRanges(): Array<{ startLine: number; endLine: number }> {
430+
const ranges: Array<{ startLine: number; endLine: number }> = [];
431+
const lineCount = this.model.getLineCount();
432+
433+
// First, get comment blocks from lexer token blocks
434+
const tokenBlocks = this.getTokenBlocks();
435+
for (const block of tokenBlocks) {
436+
const isComment = block.blockComment === true || block.type === "comment";
437+
if (isComment && block.endLine > block.startLine) {
438+
ranges.push({
439+
startLine: block.startLine,
440+
endLine: block.endLine,
441+
});
442+
}
443+
}
444+
445+
// Then scan for any multiline comment tokens that might not be in token blocks
446+
for (let lineNum = 0; lineNum < lineCount; lineNum++) {
447+
const tokens = this.getSyntax(lineNum);
448+
449+
for (const token of tokens) {
450+
if (token.style === "comment" || token.style === "macro-comment") {
451+
// Check if this token indicates a multiline span
452+
if (
453+
token.state &&
454+
typeof token.state === "object" &&
455+
token.state.line !== undefined
456+
) {
457+
const endLine = token.state.line;
458+
if (endLine > lineNum) {
459+
// Check if this range is already covered
460+
const isAlreadyCovered = ranges.some(
461+
(range) =>
462+
range.startLine === lineNum && range.endLine === endLine,
463+
);
464+
465+
if (!isAlreadyCovered) {
466+
ranges.push({
467+
startLine: lineNum,
468+
endLine: endLine,
469+
});
470+
}
471+
}
472+
}
473+
break; // Only need first comment token per line
474+
}
475+
}
476+
}
477+
478+
return ranges;
479+
}
426480
}

0 commit comments

Comments
 (0)