Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions javascript/packages/language-server/src/folding_range_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { FoldingRange, FoldingRangeKind } from "vscode-languageserver/node"
import { Visitor } from "@herb-tools/core"

import type {
Node,
HTMLElementNode,
HTMLOpenTagNode,
HTMLAttributeValueNode,
HTMLCommentNode,
ERBIfNode,
ERBElseNode,
ERBUnlessNode,
ERBBlockNode,
ERBWhileNode,
ERBUntilNode,
ERBForNode,
ERBBeginNode,
DocumentNode,
} from "@herb-tools/core"

export class FoldingRangeService {
getFoldingRanges(document: DocumentNode): FoldingRange[] {
const collector = new FoldingRangeCollector()

collector.visit(document)

return collector.ranges
}
}

/**
* Visitor that collects foldable ranges from the AST
*/
class FoldingRangeCollector extends Visitor {
ranges: FoldingRange[] = []

visitHTMLElementNode(node: HTMLElementNode): void {
this.addRangeForNode(node, node.body)
this.visitChildNodes(node)
}

visitHTMLOpenTagNode(node: HTMLOpenTagNode): void {
this.addRangeForNode(node, node.children)
this.visitChildNodes(node)
}

visitHTMLCommentNode(node: HTMLCommentNode): void {
const startLine = this.toZeroBased(node.location.start.line)
const endLine = this.toZeroBased(node.location.end.line)

this.addRange(startLine, endLine, FoldingRangeKind.Comment)

this.visitChildNodes(node)
}

visitHTMLAttributeValueNode(node: HTMLAttributeValueNode): void {
this.addRangeForNode(node, node.children)
this.visitChildNodes(node)
}

visitERBIfNode(node: ERBIfNode): void {
const startLine = this.toZeroBased(node.location.start.line)
const endLine = this.toZeroBased(node.location.end.line)

this.addRange(startLine, endLine)

if (node.statements.length > 0) {
const firstStatement = node.statements[0]
const lastStatement = node.statements[node.statements.length - 1]

const startLine = this.toZeroBased(firstStatement.location.start.line)
const endLine = this.toZeroBased(lastStatement.location.end.line)

this.addRange(startLine, endLine)
}

this.visitChildNodes(node)
}

visitERBElseNode(node: ERBElseNode): void {
this.addRangeForNode(node, node.statements)
this.visitChildNodes(node)
}

visitERBUnlessNode(node: ERBUnlessNode): void {
this.addRangeForNode(node, node.statements)
this.visitChildNodes(node)
}

visitERBBlockNode(node: ERBBlockNode): void {
this.addRangeForNode(node, node.body)
this.visitChildNodes(node)
}

visitERBWhileNode(node: ERBWhileNode): void {
this.addRangeForNode(node, node.statements)
this.visitChildNodes(node)
}

visitERBUntilNode(node: ERBUntilNode): void {
this.addRangeForNode(node, node.statements)
this.visitChildNodes(node)
}

visitERBForNode(node: ERBForNode): void {
this.addRangeForNode(node, node.statements)
this.visitChildNodes(node)
}

visitERBBeginNode(node: ERBBeginNode): void {
this.addRangeForNode(node, node.statements)
this.visitChildNodes(node)
}

// TODO: consider adding `startCharacter` and `endCharacter`
private addRange(startLine: number, endLine: number, kind?: FoldingRangeKind): void {
if (endLine > startLine) {
this.ranges.push({
startLine,
endLine,
kind,
})
}
}

private addRangeForNode(node: Node, children: Node[]) {
if (children.length > 0) {
const startLine = this.toZeroBased(node.location.start.line)
const endLine = this.toZeroBased(node.location.end.line)

this.addRange(startLine, endLine)
}
}

private toZeroBased(line: number): number {
return line - 1
}
}
1 change: 1 addition & 0 deletions javascript/packages/language-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./service"
export * from "./diagnostics"
export * from "./document_service"
export * from "./formatting_service"
export * from "./folding_range_service"
export * from "./project"
export * from "./settings"
export * from "./utils"
Expand Down
13 changes: 13 additions & 0 deletions javascript/packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
DocumentRangeFormattingParams,
CodeActionParams,
CodeActionKind,
TextEdit,
FoldingRangeParams,
} from "vscode-languageserver/node"

import { Service } from "./service"
Expand Down Expand Up @@ -53,6 +55,7 @@ export class Server {
codeActionProvider: {
codeActionKinds: [CodeActionKind.QuickFix, CodeActionKind.SourceFixAll]
},
foldingRangeProvider: true,
},
}

Expand Down Expand Up @@ -175,6 +178,16 @@ export class Server {

return autofixCodeActions.concat(linterDisableCodeActions)
})

this.connection.onFoldingRanges((params: FoldingRangeParams) => {
const document = this.service.documentService.get(params.textDocument.uri)

if (!document) return []

const parseResult = this.service.parserService.parseDocument(document)

return this.service.foldingRangeService.getFoldingRanges(parseResult.document)
})
}

listen() {
Expand Down
3 changes: 3 additions & 0 deletions javascript/packages/language-server/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ConfigService } from "./config_service"
import { AutofixService } from "./autofix_service"
import { CodeActionService } from "./code_action_service"
import { DocumentSaveService } from "./document_save_service"
import { FoldingRangeService } from "./folding_range_service"

import { version } from "../package.json"

Expand All @@ -30,6 +31,7 @@ export class Service {
configService: ConfigService
codeActionService: CodeActionService
documentSaveService: DocumentSaveService
foldingRangeService: FoldingRangeService

constructor(connection: Connection, params: InitializeParams) {
this.connection = connection
Expand All @@ -44,6 +46,7 @@ export class Service {
this.codeActionService = new CodeActionService(this.project, this.config)
this.diagnostics = new Diagnostics(this.connection, this.documentService, this.parserService, this.linterService, this.configService)
this.documentSaveService = new DocumentSaveService(this.connection, this.settings, this.autofixService, this.formattingService)
this.foldingRangeService = new FoldingRangeService()

if (params.initializationOptions) {
this.settings.globalSettings = params.initializationOptions as PersonalHerbSettings
Expand Down
Loading
Loading