Using DelphiLSP Code Insight with Other Editors

From RAD Studio
Jump to: navigation, search

DelphiLSP - Language Server for Delphi

DelphiLSP.exe is a Delphi language server implementing the Language Server Protocol (LSP). It encapsulates language features such as code completion and error insight. It’s used within the IDE, but can also be used with Visual Studio Code or other editors that support the LSP protocol.

Using DelphiLSP in Visual Studio Code

Use the Delphi LSP extension in Visual Studio Code to open RAD Studio projects and perform operations. Follow the steps to open your projects: Install the DelphiLSP extension for VSCode:

  • Install RAD Studio 11.0 or higher on your local machine.
  • Install Visual Studio Code.
  • Download and install the Delphi LSP extension from Visual Studio Code Marketplace.
    • Reload the Visual Studio Code window or use the command palette <F1> and type "reload window".

To open a project:

  • Ensure Delphi creates a .delphilsp.json file for the Delphi project you want to work with in Visual Studio Code. For detailed information, see Code Insight Reference.
Note: Do not check the .delphilsp.json files in the source control because they contain applicable settings to the local machine config.
  • Open a folder in Visual Studio Code that has one or more Delphi projects.
    • Go to File > Open Folder to load your projects.
Note: Open the full project, not single files.

Loaded project.png

The DelphiLSP extension will use the .delphilsp.json file in the folder you open. If there are none or multiple, you will be shown a message prompting you to choose a .delphilsp.json file to use. See ‘Setting Project Config Files Manually’ below.

Note: The Delphi LSP extension is activated only when you open a Delphi project (.pas or .dpr file).
  • Start using LSP features with your Delphi code in Visual Studio Code. You can see that Code Insight features are available, the same as in RAD Studio. For more information, see Code Insight Reference.

Delphi code.png

Troubleshooting

Installing DelphiLSP Extension in Visual Studio Code manually

Install the Delphi LSP extension manually in Visual Studio Code following the steps:

  • Go to Extensions and Install from VSIX the DelphiLSP extension file (ex. delphiLsp-x.x.x.vsix).

Install from VSIX.png

  • Open the User Settings and set the Delphi Lsp:Executable:
    • Go to File > Preferences > Settings
    • In the search bar look for delphilsp.
    • Make sure you are on the User tab.
    • Set the Delphi Lsp:Executable with the Path to DelphiLSP.exe

SETTINGS.png

Setting project files manually

When no config files are found, or multiple files are found in Visual Studio Code, a small prompt appears. Click on it to open the plugin settings.

Workspace settings.png

The Settings File must point at the .delphilsp.json configuration of your project.

Note: Configure your project in the Workspace Settings tab. For more information, see Visual Studio Code documentation on User vs Workspace settings.

Filling Bug Reports

The .log and .trace files contain important information about system operations. It is helpful to check and analyze them when experiencing errors or undesired behavior.

To debug issues and file bug reports:

  • In the DelphiLSP settings, turn on logging. Set Log Modes to 255 (a bitmask for all info).
  • Follow the same steps to file a Delphi LSP bug report.
  • Make sure you attach the .delphilsp.json file you are using.
Note: The .trace and .log files are only created when logging is activated.

Visual Studio Code Debugging

In DelphiLSP projects, the .log files are not created but the DelphiLSP extension allows you to check them through Visual Studio Code.

To debug issues through Visual Studio Code:

  • Open the Output View through the Visual Studio Code window or use the command palette <F1> and type "focus output view".
  • Select DelphiLSP in the drop-down menu.
  • A log message is sent from the server to the client in real-time.

Log Message.png

Using DelphiLSP with Other Editors

The following documentation may be useful if you want to test DelphiLSP with other editors. DelphiLSP only communicates over standard (console) I/O. The LSP server will look for a .delphilsp.json file the same as when it is used from within VSCode.

Initialization

LSP allows custom initializationOptions within the initialize request. This is an example of a valid initialization request:

{
   "jsonrpc":"2.0",
   "id":1,
   "method":"initialize",
   "params":{
      "processId":4711,
      "rootUri":"file:///C%3A/Test",
      "initializationOptions":{
         "serverType":"controller",
         "agentCount":2,
         "returnDccFlags":true,
         "returnHoverModel":true,
         "storeProjectSettings":false
      }
   }
}

The fields are as follows:

serverType

Defines the operation mode of DelphiLSP:
"controller": The server will spawn agentCount DelphiLSP sub-processes (agents) that work with the controller in order to answer requests. The controller acts as a proxy between the IDE and the agents. When an agent terminates or stops responding to requests, the controller will replace it with a new fresh agent. Requests can be processed by multiple agents in parallel.
Note: We only support, and have only tested, configurations with up to two-agent processes. One is used for Error Insight, and the other to handle all other LSP requests, such as code completion. This is one agent and one linter.
"agent": The server runs in single-process mode. The server can only handle one request at a time. It is not possible to get error insight (diagnostics) in this mode.
"linter": The server runs in single-process mode. The server cannot process any language feature requests such as code completion. It is dedicated to error insight. Errors, warnings and hints will be provided through the textDocument/publishDiagnostics notification
The default value is "agent". Use “controller” paired with an agentCount of 2 to get the same behavior as the Delphi IDE and the default behavior of the VSCode extension.

agentCount

Defines the number of sub-processes to be spawned when serverType = "controller".
agentCount <= 0: agentCount will be set to 1
agentCount = 1: One sub process with serverType = "agent" will be spawned
agentCount > 1: [agentCount - 1] sub-processes with serverType = "agent" will be spawned. One sub process with serverType = "linter" will be spawned.
The default value is 2.

returnDccFlags

If true then code completion items will carry some additional information from the compiler. The IDE uses this data to display types of code completion that are not defined in the LSP specification, such as procedure or type.
The default value is false.

returnHoverModel

If true then textDocument/hover will return a JSON object instead of text/Markdown.
The default value is false.

storeProjectSettings

If true then the settings sent through workspace/didChangeConfiguration will be generated and saved to %currentDelphiProject%.delphilsp.json. When DelphiLSP is used with a third-party editor then it can use this file to get project settings. See the configuration.settingsFile property.
This flag will only be evaluated when used by RAD Studio and is how Delphi causes the .delphilsp.json file to be created.
The default value is false.

enableFileWatcher

If true then DelphiLSP watches directories for changes in order to detect file modifications. If changes are detected, it refreshes based on the new contents of the file. This only has an effect for files that are not open in the editor: for files that are open in the editor, the editor is responsible for ensuring the LSP server has the current file contents.
The default value is true.

Configurations

LSP allows custom settings within the workspace/didChangeConfiguration notification, and the following is an example of a valid notification:

{
	"jsonrpc": "2.0",
	"method": "workspace/didChangeConfiguration",
	"params": {
	    	"settings": {
     	   		"settingsFile": "file:///C%3A/Test/SampleProject.delphilsp.json"
    		}
	}
}

settingsFile

It is the URI of a .delphilsp.json settings file that has previously been created with storeProjectSettings enabled. This happens when the Delphi IDE ‘Generate LSP Config’ setting is checked in the Code Insight options.
The ability to load settings from a configuration file is only active when DelphiLSP is used by third-party editors. When used from Delphi, the IDE communicates directly.