serve-d
Join the chat:
Microsoft language server protocol implementation for D.
This program is the heart of code-d and implements most IDE features in D.
The purpose of this project is to give every editor the same capabilities and editing features as code-d through the widely available Microsoft Language Server Protocol (LSP).
Looking for your Editor? Check out the Usage section
Special Thanks
Corporate Sponsors
Development on code-d/serve-d tools is sponsored by
Weka.IO |
If you are looking for a Job with D, check out the companies listed above!
For professional D consulting, programming services or corporate support with D IDE tools, DUB and the ecosystem, contact me through [email protected]
Individual Sponsors
Thanks to the following big GitHub sponsors financially supporting the code-d/serve-d tools:
@zyebytevt |
Features
See code-d wiki
This implements most language features of the language server protocol for D and also a lot of support for other D related files such as vibe.d diet files and DlangUI DML files.
Usage
To use serve-d you will need an Editor (client) with support for the Language Server Protocol. Depending on the editor the initialization code might look different and will need to be adapted for each editor. This is relatively small though and can be done easily for all editors.
Officially Supported Editors
add LSP configurations using serve-d for other editors and PR them here!
Other Editor Guides
Project Layout
serve-d:http
- Downloads a file over HTTP with progress using WinHTTP on Windows and the dub requests library on other platforms.serve-d:lsp
- LSP protocol types and RPC primitivesserve-d:serverbase
- LSP server basics for quickly creating an LSP server for any language.serve-d:dcd
- DCD client using direct communication (without spawning dcd-client) for lower latencyserve-d:workspace-d
- Implementation of core D functionality and source code processing using libdparse, dfmt, D-Scanner and DCD.source/
- Maps LSP commands and state to serve-d:workspace-d functionality.
Command Line options
serve-d offers the following command line options to allow the LSP client to configure serve-d on startup:
Option | Default | Description |
---|---|---|
-r / --require |
[] | (allows multiple, optional) List of options which this serve-d version needs to implement. serve-d will intentionally crash on startup if one of the given options is not supported by this version. Specifying or not specifying them will not change anything on runtime. Implemented features: d (PoC / unneeded), workspaces (multi-workspace support) |
-p / --provide |
[] | (allows multiple, optional) List of options to affect behavior of serve-d. Use this to indicate support for special editor features/extensions not covered by the LSP spec. Supported values: (see provides list below) |
-v / --version |
n/a | Prints the version to stdout and exists |
--logfile |
n/a | Overrides the logging output to log to a file instead of stderr for debug logs |
--loglevel |
verbose for debug/nightly builds, otherwise info |
Changes the minimum log level when messages are logged. See serve-d --help for possible values |
--lang |
en |
Changes the language of GUI messages to a supported translation |
--wait |
false |
Waits for one second before starting (useful to be able to attach when debugging) |
--provide
values
The following provide values are supported by serve-d and will improve interop with the editor:
--provide http
When this provide flag is set, serve-d will send a request to the client when it wants to download a file (commonly DCD server files) using the coded/interactiveDownload
request.
If this is not set, an internal download function is used which calls coded/logInstall
on the client on every progress event.
--provide implement-snippets
If this is set, the served/implementMethods
request will return TextEdits with snippet strings inside of them as defined by vscode.
--provide context-snippets --provide default-snippets
If this is set, auto completion requests will also return built-in and custom defined snippets. Will register snippets for popular DUB dependencies that are built into serve-d.
NOTE: these flags will be enabled by default in serve-d v0.9.0 and become obsolete then.
Custom requests/notifications
serve-d defines a variety of custom requests and notifications for better editor integration. All requests starting with coded/
are sent from serve-d to the client and all requests starting with served/
are sent from client to serve-d at any time.
serve-d internally handles an active instance which is the instance where last a relevant command has been run. (such as auto complete) It will be used for some commands.
served/sortImports
Request Parameter: SortImportsParams
Returns: TextEdit[]
Command to sort all user imports in a block at a given position in given code. Returns a list of changes to apply. (Replaces the whole block currently if anything changed, otherwise empty)
interface SortImportsParams
{
/** Text document to look in */
textDocument: TextDocumentIdentifier;
/** Location of cursor as standard offset */
location: number;
}
served/implementMethods
Request Parameter: ImplementMethodsParams
Returns: TextEdit[]
Implements the interfaces or abstract classes of a specified class/interface. The given position must be on/inside the identifier of any subclass after the colon (:
) in a class definition.
interface ImplementMethodsParams
{
/** Text document to look in */
textDocument: TextDocumentIdentifier;
/** Location of cursor as standard offset */
location: number;
}
served/restartServer
Request Parameter: none
Returns: true
Restarts all DCD servers started by this serve-d instance. Returns true
once done.
served/killServer
Notification Parameter: none
Kills all DCD servers started by this serve-d instance.
served/addDependencySnippet
Request Parameter: AddDependencySnippetParams
Returns: boolean
Registers a snippet across the whole serve-d application which may be limited to given grammatical scopes.
Requires --provide context-snippets
Returns false if SnippetsComponent hasn't been loaded yet, otherwise true.
served/updateDCD
Notification Parameter: none
Manually triggers a DCD update either by compiling from source or downloading prebuilt binaries depending on the host system and serve-d. Excessively calls the coded/logInstall
notification.
served/listConfigurations
Request Parameter: none
Returns: string[]
Returns an empty array if there is no active instance or if it doesn't have dub.
Otherwise returns the names of available configurations in dub.
served/switchConfig
Request Parameter: string
Returns: bool
Sets the current dub configuration for building and other tools. Returns true on success.
served/getConfig
Request Parameter: none
Returns: string
Returns the current dub configuration or null if there is no dub in the active instance.
served/listArchTypes
Request Parameter: ListArchTypesParams
Returns: string[] | ArchTypeInfo[]
Returns an empty array if there is no active instance or if it doesn't have dub.
Otherwise returns the names of available architectures in dub. (e.g. x86 or x86_64)
interface ListArchTypesParams
{
/** If true, return ArchTypeInfo[] with meanings instead of string[] */
withMeaning?: boolean;
}
interface ArchTypeInfo
{
/** The value to use with a switchArchType call / the value DUB uses. */
value: string;
/** If not null, show this string in the UI rather than value. */
label: string | null;
}
served/switchArchType
Request Parameter: string
Returns: bool
Sets the current architecture for building and other tools. Returns true on success.
served/getArchType
Request Parameter: none
Returns: string
Returns the current dub architecture or null if there is no dub in the active instance.
served/listBuildTypes
Request Parameter: none
Returns: string[]
Returns an empty array if there is no active instance or if it doesn't have dub.
Otherwise returns the names of available build types in dub.
served/switchBuildType
Request Parameter: string
Returns: bool
Sets the current dub build type for building and other tools. Returns true on success.
served/getBuildType
Request Parameter: none
Returns: string
Returns the current dub build type or null if there is no dub in the active instance.
served/getCompiler
Request Parameter: none
Returns: string
Returns the name of the current compiler.
served/switchCompiler
Request Parameter: string
Returns: bool
Sets the current compiler to use in dub for building and other tools. Returns true on success.
served/addImport
Request Parameter: AddImportParams
Returns: ImportModification
Parses the source code and returns code edits how to insert a given import into the code.
interface AddImportParams
{
/** Text document to look in */
textDocument: TextDocumentIdentifier;
/** The name of the import to add */
name: string;
/** Location of cursor as standard offset */
location: number;
/** if `false`, the import will get added to the innermost block */
insertOutermost?: boolean = true;
}
interface ImportModification
{
/** Set if there was already an import which was renamed. (for example import io = std.stdio; would be "io") */
rename: string;
/** Array of replacements to add the import to the code */
replacements: CodeReplacement[];
}
interface CodeReplacement
{
/**
* Range what to replace. If both indices are the same its inserting.
*
* This value is specified as bytes offset from the UTF-8 source.
*/
size_t[2] range;
/** Content to replace it with. Empty means remove. */
string content;
}
served/updateImports
Request Parameter: UpdateImportsParams
Returns: boolean
Refreshes the dub dependencies from the local filesystem. Triggers a coded/updateDubTree
notification on success and updates imports in DCD.
Returns true on success.
interface UpdateImportsParams
{
/// set this to false to not emit progress updates for the UI
reportProgress?: bool;
}
served/listDependencies
Request Parameter: string
packageName
Returns: DubDependency[]
Lists the dependencies of a given dub package name. If no package name is given (empty string) then all dependencies of the current instance will be listed.
interface DubDependency
{
/** The name of this package */
name: string;
/** The installed version of this dependency or null if it isn't downloaded/installed yet */
version: string;
/** Path to the directory in which the package resides or null if it's not stored in the local file system. */
path: string;
/** Description as given in dub package file */
description: string;
/** Homepage as given in dub package file */
homepage: string;
/** Authors as given in dub package file */
authors: string[];
/** Copyright as given in dub package file */
copyright: string;
/** License as given in dub package file */
license: string;
/** List of the names of subPackages as defined in the package */
subPackages: string[];
/** `true` if this dependency has other dependencies */
hasDependencies: boolean;
/** `true` if no package name was given and thus this dependency is a root dependency of the active project. */
root: boolean;
}
served/buildTasks
Request Parameter: none
Returns: Task[]
Returns a list of build tasks for all dub instances in the project. Currently each with Build, Run, Rebuild and Test commands.
enum TaskGroup
{
clean = "clean",
build = "build",
rebuild = "rebuild",
test = "test"
}
interface Task
{
/// the default JSON task
definition: any;
/// global | workspace | uri of workspace folder
scope: string;
/// command to execute
exec: string[];
/// name of the task
name: string;
/// true if this is a background task without shown console
isBackground: boolean;
/// Task source extension name
source: string;
/// clean | build | rebuild | test
group: TaskGroup;
/// problem matchers to use
problemMatchers: string[];
}
served/convertDubFormat
Notification Params: DubConvertRequest
Starts a conversion of a dub.json/dub.sdl file to a given other format. Shows an error message in the UI if unsuccessful and triggers a workspace/applyEdit
command when successful with the new content.
interface DubConvertRequest
{
/** Text document to look in */
textDocument: TextDocumentIdentifier;
/** The format to convert the dub recipe to. (json, sdl) */
newFormat: string;
}
served/installDependency
Notification Params: InstallRequest
Adds a dependency to the dub recipe file of the currently active instance (respecting indentation) and calls dub upgrade and updates imports afterwards.
Writes changes to the file system.
interface InstallRequest
{
/** Name of the dub dependency */
name: string;
/** Version to install in the dub recipe file */
version: string;
}
served/updateDependency
Notification Params: UpdateRequest
Changes a dependency in the dub recipe file of the currently active instance (respecting indentation) to the given version and calls dub upgrade and updates imports afterwards.
Does nothing if the dependency wasn't found in the dub recipe.
Writes changes to the file system.
interface UpdateRequest
{
/** Name of the dub dependency */
name: string;
/** Version to install in the dub recipe file */
version: string;
}
served/uninstallDependency
Notification Params: UninstallRequest
Removes a dependency from the dub recipe file of the currently active instance and calls dub upgrade and updates imports afterwards.
Writes changes to the file system.
interface UninstallRequest
{
/** Name of the dub dependency */
name: string;
}
served/doDscanner
Notification Params: DocumentLinkParams
Manually triggers DScanner linting on the given file. (respecting user configuration)
served/searchFile
Request Params: string
query
Returns: string[]
Searches for a given filename (optionally also with subfolders) and returns all locations in the project and all dependencies including standard library where this file exists.
served/findFilesByModule
Request Params: string
module
Returns: string[]
Lists all files with a given module name in the project and all dependencies and standard library.
served/getDscannerConfig
Request Params: DocumentLinkParams
Returns: DScannerIniSection[]
Returns the current D-Scanner configuration for a given URI.
/// An ini section of the dscanner.ini which is written in form [name]
interface DScannerIniSection
{
/// A textual human readable description of the section
description: string;
/// The name of the section as written in the ini
name: string;
/// Features which are children of this section
features: DScannerIniFeature[]
}
/// A single feature in a dscanner.ini which can be turned on/off
interface DScannerIniFeature
{
/// A textual human readable description of the value
description: string;
/// The name of the value
name: string;
/// Enables/disables the feature or enables it with being disabled in unittests
enabled: "disabled" | "enabled" | "skip-unittest"
}
served/getActiveDubConfig
Request Params: none
Returns dub information for the currently active project (dub project where last file was edited / opened / etc)
Returns: at least
{
"packagePath": string,
"packageName": string,
"targetPath": string,
"targetName": string,
"workingDirectory": string,
"mainSourceFile": string,
"dflags": string[],
"lflags": string[],
"libs": string[],
"linkerFiles": string[],
"sourceFiles": string[],
"copyFiles": string[],
"versions": string[],
"debugVersions": string[],
"importPaths": string[],
"stringImportPaths": string[],
"importFiles": string[],
"stringImportFiles": string[],
"preGenerateCommands": string[],
"postGenerateCommands": string[],
"preBuildCommands": string[],
"postBuildCommands": string[],
"preRunCommands": string[],
"postRunCommands": string[]
}
served/getProfileGCEntries
Request Params: none
Returns all profilegc.log entries parsed and combined.
Returns: ProfileGCEntry[]
interface ProfileGCEntry
{
bytesAllocated: number;
allocationCount: number;
type: string; /// the function and/or type name
uri: string; /// absolute, normalized uri
displayFile: string; /// as parsed from file
line: number; /// 1-based line number
}
served/didConfigurationChange
Notification Params: Exactly as for workspace/didConfigurationChange
This notification triggers the use of alternate configuration notifications.
Once this is received, the server will ignore workspace/didConfigurationChange
notifications. This mechanisms exists to support some client/plugin combinations
where the plugin needs more direct control over the configuration.
served/getInfo
Request Params: ServedInfoParams
Returns all profilegc.log entries parsed and combined.
Returns: ServedInfoResponse
interface ServedInfoParams
{
includeConfig?: boolean;
includeIndex?: boolean;
}
interface ServedInfoResponse
{
/** Same as in the initialized response. (the LSP server info) */
serverInfo: ServerInfo;
/**
* Only included if `ServedInfoParams.includeConfig` is true.
*
* Contains the entire config object, e.g. `{"d":{...}, ...}`
*/
currentConfiguration?: Configuration;
/**
* Only included if `ServedInfoParams.includeIndex` is true.
* Key: module, Value: modules that depend on the (key) module.
* This is only the module index for the active workspace.
*/
{ [module: string]: string[] }?: moduleIndex;
/**
* Describes the global workspace, same type as in
* `coded/changedSelectedWorkspace`
*/
globalWorkspace: WorkspaceState;
/** Describes all available workspaces. */
workspaces: WorkspaceState[];
/**
* First index inside the `workspaces` array sent along this value, where
* `selected` is set to true, or -1 for global workspace.
*/
selectedWorkspaceIndex: number;
}
served/forceLoadProjects
Request Params: string[]
Forces the load of projects, regardless of manyProjects limit or action configuration of the given file paths. Returns true if successful, false otherwise, for each item in the params array in order that was given in.
Returns: bool[]
coded/updateSetting
Client notification Params: UpdateSettingParams
Tells the client to update a user or workspace setting. This is done for updating the dcdClientPath and dcdServerPath on installation.
interface UpdateSettingParams
{
/** The configuration section to update in (e.g. "d" or "dfmt") */
section: string;
/** The value to set the configuration value to */
value: any;
/** `true` if this is a configuration change across all instances and not just the active one */
global: bool;
}
coded/logInstall
Client notification Params: string
message
Instructs the client to log a message that has something to do with the installation routine of serve-d or dependencies.
coded/initDubTree
Client notification Params: none
Tells the client that dub has been loaded and the dependency tree can now be fetched.
coded/updateDubTree
Client notification Params: none
Tells the client that dub dependencies have been reloaded and should be redisplayed.
coded/changedSelectedWorkspace
Client notification Params: WorkspaceState
Tells the client when the active instance changed.
interface WorkspaceState
{
/** URI to the workspace folder */
uri: string;
/** name of the workspace folder (or internal placeholder) */
name: string;
/** true if this instance has been initialized */
initialized: boolean;
/** true if this is the active instance */
selected: boolean;
/**
* May contain errors that are pending and will be shown once the user with
* this workspace.
*
* Key: URI folder or file in which startup issues occured. (compare with
* startsWith)
* Value: human readable message.
* */
pendingErrors: { [folderUri: string]: string };
}
coded/skippedLoads
Client notification Params: InteractiveDownload
Tells the client that project loading was skipped for the given path(s). The client may then ask the user or query configuration if the paths should be loaded or skipped. When requesting to load projects, pass these roots as arguments to the reqeust served/forceLoadProjects
.
Otherwise, project loading is blocking and asks the client using a message box for each lazy-loaded project. IDE functionality is otherwise limited while these message boxes are open.
This must be implemented if --provide async-ask-load
is given in the command line, otherwise this is not called.
interface SkippedLoadsNotification
{
/** List of folder file paths */
roots: string[];
}
coded/interactiveDownload
Client request Params: InteractiveDownload
Returns: boolean
Instructs the client to download a file into a given output path using download UI.
This must be implemented if --provide http
is given in the command line, otherwise this is not called.
interface InteractiveDownload
{
/** The URL to download */
url: string;
/** The title to show in the UI popup for this download */
title?: string;
/** The file path to write the downloaded file to */
output: string;
}
User Configuration
The server has support for configuration for these items.
Installation
If you use an existing extension (code-d) you will not need to do these steps
If you want to manually get the serve-d binaries or if you want to add installation support for your editor, check out the sections below. The extension code-d already does this automatically and there is no need to do it there.
Installing a pre-built binary:
Grab latest stable ore pre-release with binaries from GitHub releases
Grab latest nightly binaries from GitHub releases
Manually building from source:
dub build
Issues
If you have issues with any editors using serve-d, please report an issue