Skip to content
Open
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
2 changes: 2 additions & 0 deletions javascript/packages/language-server/src/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Capabilities {
readonly hasConfiguration: boolean
readonly hasWorkspaceFolders: boolean
readonly hasDiagnosticRelatedInformation: boolean
readonly hasApplyEdit: boolean
readonly hasShowDocument: boolean

constructor(params: InitializeParams) {
Expand All @@ -28,6 +29,7 @@ export class Capabilities {

this.hasConfiguration = !!this.client.workspace?.configuration
this.hasWorkspaceFolders = !!this.client.workspace?.workspaceFolders
this.hasApplyEdit = !!this.client.workspace?.applyEdit
this.hasShowDocument = !!this.client.window?.showDocument
this.hasDiagnosticRelatedInformation = !!this.client.textDocument?.publishDiagnostics?.relatedInformation
}
Expand Down
5 changes: 5 additions & 0 deletions javascript/packages/language-server/src/on_type_formatting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { DocumentOnTypeFormattingOptions } from "vscode-languageserver/node"

export const ON_TYPE_FORMATTING_OPTIONS: DocumentOnTypeFormattingOptions = {
firstTriggerCharacter: ">",
}
49 changes: 49 additions & 0 deletions javascript/packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Connection,
DocumentFormattingParams,
DocumentRangeFormattingParams,
DocumentOnTypeFormattingParams,
CodeActionParams,
CodeActionKind,
FoldingRangeParams,
Expand All @@ -32,6 +33,7 @@ import { Config } from "@herb-tools/config"
import { isPartialPath } from "@herb-tools/analysis"
import { isConfigDocument, isPathInside } from "./utils"
import { serverVersion } from "./build_info"
import { ON_TYPE_FORMATTING_OPTIONS } from "./on_type_formatting"

import type { FileEvent } from "vscode-languageserver/node"
import type { ExtractToPartialResult } from "@herb-tools/language-service"
Expand Down Expand Up @@ -73,6 +75,7 @@ export class Server {
},
documentFormattingProvider: true,
documentRangeFormattingProvider: true,
documentOnTypeFormattingProvider: ON_TYPE_FORMATTING_OPTIONS,
codeActionProvider: {
codeActionKinds: [CodeActionKind.QuickFix, CodeActionKind.SourceFixAll, CodeActionKind.RefactorRewrite, CodeActionKind.RefactorExtract]
},
Expand Down Expand Up @@ -199,6 +202,52 @@ export class Server {
return this.session.projects.get(params.textDocument.uri)?.formattingProvider.formatRange(params) ?? []
})

this.connection.onDocumentOnTypeFormatting(async (params: DocumentOnTypeFormattingParams) => {
const document = this.session.documents.get(params.textDocument.uri)

if (!document) return []

const formatting = this.session.onTypeFormattingProvider.getFormatting(
document,
params.position,
params.ch,
params.options,
)

if (
formatting.cursor === null ||
!this.session.capabilities.hasApplyEdit ||
!this.session.capabilities.hasShowDocument
) {
return formatting.edits
}

const applyResult = await this.connection.workspace.applyEdit({
changes: { [document.uri]: formatting.edits },
})

if (!applyResult.applied) return formatting.edits

try {
const showResult = await this.connection.window.showDocument({
uri: document.uri,
takeFocus: true,
selection: {
start: formatting.cursor,
end: formatting.cursor,
},
})

if (!showResult.success) {
this.connection.console.warn("Failed to move the cursor after on-type formatting")
}
} catch (error) {
this.connection.console.error(`Failed to move the cursor after on-type formatting: ${error}`)
}

return []
})

this.connection.onDocumentHighlight((params: DocumentHighlightParams) => {
const document = this.session.documents.get(params.textDocument.uri)

Expand Down
17 changes: 16 additions & 1 deletion javascript/packages/language-server/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@ import { Capabilities } from "./capabilities"
import { WorkspaceFolders } from "./workspace_folders"
import { Documents } from "./documents"
import { DiagnosticsPublisher } from "./diagnostics_publisher"
import { ParserService, FoldingRangeProvider, SelectionRangeProvider, DocumentHighlightProvider, InlayHintProvider, HoverProvider, RewriteCodeActionProvider, CommentProvider, DocumentSymbolProvider, ExtractCodeActionProvider, DefinitionProvider } from "@herb-tools/language-service"
import {
CommentProvider,
DefinitionProvider,
DocumentHighlightProvider,
DocumentSymbolProvider,
ExtractCodeActionProvider,
FoldingRangeProvider,
HoverProvider,
InlayHintProvider,
OnTypeFormattingProvider,
ParserService,
RewriteCodeActionProvider,
SelectionRangeProvider,
} from "@herb-tools/language-service"
import { ConfigService } from "./config_service"
import { SaveOrchestrator } from "./save_orchestrator"

Expand Down Expand Up @@ -42,6 +55,7 @@ export class Session {
definitionProvider: DefinitionProvider
commentProvider: CommentProvider
documentSymbolProvider: DocumentSymbolProvider
onTypeFormattingProvider: OnTypeFormattingProvider

constructor(connection: Connection, params: InitializeParams) {
this.connection = connection
Expand Down Expand Up @@ -77,6 +91,7 @@ export class Session {
this.rewriteCodeActionProvider = new RewriteCodeActionProvider(this.parserService, process.cwd())
this.commentProvider = new CommentProvider(this.parserService)
this.documentSymbolProvider = new DocumentSymbolProvider(this.parserService)
this.onTypeFormattingProvider = new OnTypeFormattingProvider(this.parserService)

this.extractCodeActionProvider = new ExtractCodeActionProvider(this.parserService, this.capabilities, existsSync)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("Capabilities", () => {

expect(capabilities.hasConfiguration).toBe(false)
expect(capabilities.hasWorkspaceFolders).toBe(false)
expect(capabilities.hasApplyEdit).toBe(false)
expect(capabilities.hasShowDocument).toBe(false)
expect(capabilities.hasDiagnosticRelatedInformation).toBe(false)
})
Expand All @@ -26,14 +27,19 @@ describe("Capabilities", () => {
const capabilities = new Capabilities({
...mockParams,
capabilities: {
workspace: { configuration: true, workspaceFolders: true },
workspace: {
applyEdit: true,
configuration: true,
workspaceFolders: true,
},
window: { showDocument: { support: true } },
textDocument: { publishDiagnostics: { relatedInformation: true } }
}
})

expect(capabilities.hasConfiguration).toBe(true)
expect(capabilities.hasWorkspaceFolders).toBe(true)
expect(capabilities.hasApplyEdit).toBe(true)
expect(capabilities.hasShowDocument).toBe(true)
expect(capabilities.hasDiagnosticRelatedInformation).toBe(true)
})
Expand Down
Loading
Loading