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).
Special Thanks
Thanks to the following big GitHub sponsors financially supporting the code-d/serve-d tools:
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.
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 primitives
serve-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 latency
serve-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
If this is set, auto completion requests will also return built-in and custom defined snippets.
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.
Request served/sortImports
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)
interfaceSortImportsParams{/** Text document to look in */textDocument: TextDocumentIdentifier;/** Location of cursor as standard offset */location: number;}
Request served/implementMethods
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.
interfaceImplementMethodsParams{/** Text document to look in */textDocument: TextDocumentIdentifier;/** Location of cursor as standard offset */location: number;}
Request served/restartServer
Parameter: none
Returns: true
Restarts all DCD servers started by this serve-d instance. Returns true once done.
Notification served/killServer
Parameter: none
Kills all DCD servers started by this serve-d instance.
Request served/addDependencySnippet
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.
Notification served/updateDCD
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.
Request served/listConfigurations
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.
Request served/switchConfig
Parameter: string
Returns: bool
Sets the current dub configuration for building and other tools. Returns true on success.
Request served/getConfig
Parameter: none
Returns: string
Returns the current dub configuration or null if there is no dub in the active instance.
Request served/listArchTypes
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)
interfaceListArchTypesParams{/** If true, return ArchTypeInfo[] with meanings instead of string[] */withMeaning?: boolean;}interfaceArchTypeInfo{/** 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;}
Request served/switchArchType
Parameter: string
Returns: bool
Sets the current architecture for building and other tools. Returns true on success.
Request served/getArchType
Parameter: none
Returns: string
Returns the current dub architecture or null if there is no dub in the active instance.
Request served/listBuildTypes
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.
Request served/switchBuildType
Parameter: string
Returns: bool
Sets the current dub build type for building and other tools. Returns true on success.
Request served/getBuildType
Parameter: none
Returns: string
Returns the current dub build type or null if there is no dub in the active instance.
Request served/getCompiler
Parameter: none
Returns: string
Returns the name of the current compiler.
Request served/switchCompiler
Parameter: string
Returns: bool
Sets the current compiler to use in dub for building and other tools. Returns true on success.
Request served/addImport
Parameter: AddImportParams
Returns: ImportModification
Parses the source code and returns code edits how to insert a given import into the code.
interfaceAddImportParams{/** 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;}interfaceImportModification{/** 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[];}interfaceCodeReplacement{/** * 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. */stringcontent;}
Request served/updateImports
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.
interfaceUpdateImportsParams{/// set this to false to not emit progress updates for the UIreportProgress?: bool;}
Request served/listDependencies
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.
interfaceDubDependency{/** 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;}
Request served/buildTasks
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.
enumTaskGroup{clean="clean",build="build",rebuild="rebuild",test="test"}interfaceTask{/// the default JSON taskdefinition: any;/// global | workspace | uri of workspace folderscope: string;/// command to executeexec: string[];/// name of the taskname: string;/// true if this is a background task without shown consoleisBackground: boolean;/// Task source extension namesource: string;/// clean | build | rebuild | testgroup: TaskGroup;/// problem matchers to useproblemMatchers: string[];}
Notification served/convertDubFormat
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.
interfaceDubConvertRequest{/** Text document to look in */textDocument: TextDocumentIdentifier;/** The format to convert the dub recipe to. (json, sdl) */newFormat: string;}
Notification served/installDependency
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.
interfaceInstallRequest{/** Name of the dub dependency */name: string;/** Version to install in the dub recipe file */version: string;}
Notification served/updateDependency
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.
interfaceUpdateRequest{/** Name of the dub dependency */name: string;/** Version to install in the dub recipe file */version: string;}
Notification served/uninstallDependency
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.
interfaceUninstallRequest{/** Name of the dub dependency */name: string;}
Notification served/doDscanner
Params: DocumentLinkParams
Manually triggers DScanner linting on the given file. (respecting user configuration)
Request served/searchFile
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.
Request served/findFilesByModule
Params: string module
Returns: string[]
Lists all files with a given module name in the project and all dependencies and standard library.
Request served/doDscanner
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]interfaceDScannerIniSection{/// A textual human readable description of the sectiondescription: string;/// The name of the section as written in the ininame: string;/// Features which are children of this sectionfeatures: DScannerIniFeature[]}/// A single feature in a dscanner.ini which can be turned on/offinterfaceDScannerIniFeature{/// A textual human readable description of the valuedescription: string;/// The name of the valuename: string;/// Enables/disables the feature or enables it with being disabled in unittestsenabled: "disabled"|"enabled"|"skip-unittest"}
Request served/getActiveDubConfig
Params: none
Returns dub information for the currently active project (dub project where last
file was edited / opened / etc)
请发表评论