Adds support for multiple language servers per language.

Language Servers are now configured in a separate table in `languages.toml`:

```toml
[langauge-server.mylang-lsp]
command = "mylang-lsp"
args = ["--stdio"]
config = { provideFormatter = true }

[language-server.efm-lsp-prettier]
command = "efm-langserver"

[language-server.efm-lsp-prettier.config]
documentFormatting = true
languages = { typescript = [ { formatCommand ="prettier --stdin-filepath ${INPUT}", formatStdin = true } ] }
```

The language server for a language is configured like this (`typescript-language-server` is configured by default):

```toml
[[language]]
name = "typescript"
language-servers = [ { name = "efm-lsp-prettier", only-features = [ "format" ] }, "typescript-language-server" ]
```

or equivalent:

```toml
[[language]]
name = "typescript"
language-servers = [ { name = "typescript-language-server", except-features = [ "format" ] }, "efm-lsp-prettier" ]
```

Each requested LSP feature is priorized in the order of the `language-servers` array.
For example the first `goto-definition` supported language server (in this case `typescript-language-server`) will be taken for the relevant LSP request (command `goto_definition`).

If no `except-features` or `only-features` is given all features for the language server are enabled, as long as the language server supports these. If it doesn't the next language server which supports the feature is tried.

The list of supported features are:

- `format`
- `goto-definition`
- `goto-declaration`
- `goto-type-definition`
- `goto-reference`
- `goto-implementation`
- `signature-help`
- `hover`
- `document-highlight`
- `completion`
- `code-action`
- `workspace-command`
- `document-symbols`
- `workspace-symbols`
- `diagnostics`
- `rename-symbol`
- `inlay-hints`

Another side-effect/difference that comes with this PR, is that only one language server instance is started if different languages use the same language server.
pull/2507/head
Philipp Mildenberger 2 years ago
parent 7f5940be80
commit 71551d395b

@ -50,7 +50,7 @@
| `:reload-all` | Discard changes and reload all documents from the source files. | | `:reload-all` | Discard changes and reload all documents from the source files. |
| `:update`, `:u` | Write changes only if the file has been modified. | | `:update`, `:u` | Write changes only if the file has been modified. |
| `:lsp-workspace-command` | Open workspace command picker | | `:lsp-workspace-command` | Open workspace command picker |
| `:lsp-restart` | Restarts the Language Server that is in use by the current doc | | `:lsp-restart` | Restarts the language servers used by the currently opened file |
| `:lsp-stop` | Stops the Language Server that is in use by the current doc | | `:lsp-stop` | Stops the Language Server that is in use by the current doc |
| `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. | | `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. |
| `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. | | `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. |

@ -9,6 +9,7 @@ below.
necessary configuration for the new language. For more information on necessary configuration for the new language. For more information on
language configuration, refer to the language configuration, refer to the
[language configuration section](../languages.md) of the documentation. [language configuration section](../languages.md) of the documentation.
A new language server can be added by extending the `[language-server]` table in the same file.
2. If you are adding a new language or updating an existing language server 2. If you are adding a new language or updating an existing language server
configuration, run the command `cargo xtask docgen` to update the configuration, run the command `cargo xtask docgen` to update the
[Language Support](../lang-support.md) documentation. [Language Support](../lang-support.md) documentation.

@ -18,6 +18,9 @@ There are three possible locations for a `languages.toml` file:
```toml ```toml
# in <config_dir>/helix/languages.toml # in <config_dir>/helix/languages.toml
[language-server.mylang-lsp]
command = "mylang-lsp"
[[language]] [[language]]
name = "rust" name = "rust"
auto-format = false auto-format = false
@ -41,8 +44,8 @@ injection-regex = "mylang"
file-types = ["mylang", "myl"] file-types = ["mylang", "myl"]
comment-token = "#" comment-token = "#"
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
language-server = { command = "mylang-lsp", args = ["--stdio"], environment = { "ENV1" = "value1", "ENV2" = "value2" } }
formatter = { command = "mylang-formatter" , args = ["--stdin"] } formatter = { command = "mylang-formatter" , args = ["--stdin"] }
language-servers = [ "mylang-lsp" ]
``` ```
These configuration keys are available: These configuration keys are available:
@ -50,6 +53,7 @@ These configuration keys are available:
| Key | Description | | Key | Description |
| ---- | ----------- | | ---- | ----------- |
| `name` | The name of the language | | `name` | The name of the language |
| `language-id` | The language-id for language servers, checkout the table at [TextDocumentItem](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentItem) for the right id |
| `scope` | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.<name>` or `text.<name>` in case of markup languages | | `scope` | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.<name>` or `text.<name>` in case of markup languages |
| `injection-regex` | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. | | `injection-regex` | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. |
| `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. See the file-type detection section below. | | `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. See the file-type detection section below. |
@ -59,7 +63,7 @@ These configuration keys are available:
| `diagnostic-severity` | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) | | `diagnostic-severity` | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) |
| `comment-token` | The token to use as a comment-token | | `comment-token` | The token to use as a comment-token |
| `indent` | The indent to use. Has sub keys `unit` (the text inserted into the document when indenting; usually set to N spaces or `"\t"` for tabs) and `tab-width` (the number of spaces rendered for a tab) | | `indent` | The indent to use. Has sub keys `unit` (the text inserted into the document when indenting; usually set to N spaces or `"\t"` for tabs) and `tab-width` (the number of spaces rendered for a tab) |
| `language-server` | The Language Server to run. See the Language Server configuration section below. | | `language-servers` | The Language Servers used for this language. See below for more information in the section [Configuring Language Servers for a language](#configuring-language-servers-for-a-language) |
| `config` | Language Server configuration | | `config` | Language Server configuration |
| `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) | | `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) |
| `formatter` | The formatter for the language, it will take precedence over the lsp when defined. The formatter must be able to take the original file as input from stdin and write the formatted file to stdout | | `formatter` | The formatter for the language, it will take precedence over the lsp when defined. The formatter must be able to take the original file as input from stdin and write the formatted file to stdout |
@ -92,31 +96,97 @@ with the following priorities:
replaced at runtime with the appropriate path separator for the operating replaced at runtime with the appropriate path separator for the operating
system, so this rule would match against `.git\config` files on Windows. system, so this rule would match against `.git\config` files on Windows.
### Language Server configuration ## Language Server configuration
Language servers are configured separately in the table `language-server` in the same file as the languages `languages.toml`
For example:
```toml
[language-server.mylang-lsp]
command = "mylang-lsp"
args = ["--stdio"]
config = { provideFormatter = true }
environment = { "ENV1" = "value1", "ENV2" = "value2" }
[language-server.efm-lsp-prettier]
command = "efm-langserver"
[language-server.efm-lsp-prettier.config]
documentFormatting = true
languages = { typescript = [ { formatCommand ="prettier --stdin-filepath ${INPUT}", formatStdin = true } ] }
```
The `language-server` field takes the following keys: These are the available options for a language server.
| Key | Description | | Key | Description |
| --- | ----------- | | ---- | ----------- |
| `command` | The name of the language server binary to execute. Binaries must be in `$PATH` | | `command` | The name or path of the language server binary to execute. Binaries must be in `$PATH` |
| `args` | A list of arguments to pass to the language server binary | | `args` | A list of arguments to pass to the language server binary |
| `timeout` | The maximum time a request to the language server may take, in seconds. Defaults to `20` | | `config` | LSP initialization options |
| `language-id` | The language name to pass to the language server. Some language servers support multiple languages and use this field to determine which one is being served in a buffer | | `timeout` | The maximum time a request to the language server may take, in seconds. Defaults to `20` |
| `environment` | Any environment variables that will be used when starting the language server `{ "KEY1" = "Value1", "KEY2" = "Value2" }` | | `environment` | Any environment variables that will be used when starting the language server `{ "KEY1" = "Value1", "KEY2" = "Value2" }` |
The top-level `config` field is used to configure the LSP initialization options. A `format` A `format` sub-table within `config` can be used to pass extra formatting options to
sub-table within `config` can be used to pass extra formatting options to [Document Formatting Requests](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-17.md#document-formatting-request--leftwards_arrow_with_hook).
[Document Formatting Requests](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#document-formatting-request--leftwards_arrow_with_hook).
For example with typescript: For example with typescript:
```toml ```toml
[[language]] [language-server.typescript-language-server]
name = "typescript"
auto-format = true
# pass format options according to https://github.com/typescript-language-server/typescript-language-server#workspacedidchangeconfiguration omitting the "[language].format." prefix. # pass format options according to https://github.com/typescript-language-server/typescript-language-server#workspacedidchangeconfiguration omitting the "[language].format." prefix.
config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } } config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } }
``` ```
### Configuring Language Servers for a language
The `language-servers` attribute in a language tells helix which language servers are used for this language.
They have to be defined in the `[language-server]` table as described in the previous section.
Different languages can use the same language server instance, e.g. `typescript-language-server` is used for javascript, jsx, tsx and typescript by default.
In case multiple language servers are specified in the `language-servers` attribute of a `language`,
it's often useful to only enable/disable certain language-server features for these language servers.
For example `efm-lsp-prettier` of the previous example is used only with a formatting command `prettier`,
so everything else should be handled by the `typescript-language-server` (which is configured by default)
The language configuration for typescript could look like this:
```toml
[[language]]
name = "typescript"
language-servers = [ { name = "efm-lsp-prettier", only-features = [ "format" ] }, "typescript-language-server" ]
```
or equivalent:
```toml
[[language]]
name = "typescript"
language-servers = [ { name = "typescript-language-server", except-features = [ "format" ] }, "efm-lsp-prettier" ]
```
Each requested LSP feature is priorized in the order of the `language-servers` array.
For example the first `goto-definition` supported language server (in this case `typescript-language-server`) will be taken for the relevant LSP request (command `goto_definition`).
If no `except-features` or `only-features` is given all features for the language server are enabled.
If a language server itself doesn't support a feature the next language server array entry will be tried (and so on).
The list of supported features are:
- `format`
- `goto-definition`
- `goto-declaration`
- `goto-type-definition`
- `goto-reference`
- `goto-implementation`
- `signature-help`
- `hover`
- `document-highlight`
- `completion`
- `code-action`
- `workspace-command`
- `document-symbols`
- `workspace-symbols`
- `diagnostics`
- `rename-symbol`
- `inlay-hints`
## Tree-sitter grammar configuration ## Tree-sitter grammar configuration
The source for a language's tree-sitter grammar is specified in a `[[grammar]]` The source for a language's tree-sitter grammar is specified in a `[[grammar]]`

@ -43,6 +43,7 @@ pub struct Diagnostic {
pub message: String, pub message: String,
pub severity: Option<Severity>, pub severity: Option<Severity>,
pub code: Option<NumberOrString>, pub code: Option<NumberOrString>,
pub language_server_id: usize,
pub tags: Vec<DiagnosticTag>, pub tags: Vec<DiagnosticTag>,
pub source: Option<String>, pub source: Option<String>,
pub data: Option<serde_json::Value>, pub data: Option<serde_json::Value>,

@ -17,7 +17,7 @@ use std::{
borrow::Cow, borrow::Cow,
cell::RefCell, cell::RefCell,
collections::{HashMap, VecDeque}, collections::{HashMap, VecDeque},
fmt, fmt::{self, Display},
hash::{Hash, Hasher}, hash::{Hash, Hasher},
mem::{replace, transmute}, mem::{replace, transmute},
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -60,8 +60,11 @@ fn default_timeout() -> u64 {
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Configuration { pub struct Configuration {
pub language: Vec<LanguageConfiguration>, pub language: Vec<LanguageConfiguration>,
#[serde(default)]
pub language_server: HashMap<String, LanguageServerConfiguration>,
} }
impl Default for Configuration { impl Default for Configuration {
@ -75,7 +78,10 @@ impl Default for Configuration {
#[serde(rename_all = "kebab-case", deny_unknown_fields)] #[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LanguageConfiguration { pub struct LanguageConfiguration {
#[serde(rename = "name")] #[serde(rename = "name")]
pub language_id: String, // c-sharp, rust pub language_id: String, // c-sharp, rust, tsx
#[serde(rename = "language-id")]
// see the table under https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentItem
pub language_server_language_id: Option<String>, // csharp, rust, typescriptreact, for the language-server
pub scope: String, // source.rust pub scope: String, // source.rust
pub file_types: Vec<FileType>, // filename extension or ends_with? <Gemfile, rb, etc> pub file_types: Vec<FileType>, // filename extension or ends_with? <Gemfile, rb, etc>
#[serde(default)] #[serde(default)]
@ -85,9 +91,6 @@ pub struct LanguageConfiguration {
pub text_width: Option<usize>, pub text_width: Option<usize>,
pub soft_wrap: Option<SoftWrap>, pub soft_wrap: Option<SoftWrap>,
#[serde(default, skip_serializing, deserialize_with = "deserialize_lsp_config")]
pub config: Option<serde_json::Value>,
#[serde(default)] #[serde(default)]
pub auto_format: bool, pub auto_format: bool,
@ -107,8 +110,8 @@ pub struct LanguageConfiguration {
#[serde(skip)] #[serde(skip)]
pub(crate) highlight_config: OnceCell<Option<Arc<HighlightConfiguration>>>, pub(crate) highlight_config: OnceCell<Option<Arc<HighlightConfiguration>>>,
// tags_config OnceCell<> https://github.com/tree-sitter/tree-sitter/pull/583 // tags_config OnceCell<> https://github.com/tree-sitter/tree-sitter/pull/583
#[serde(skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Vec::is_empty")]
pub language_server: Option<LanguageServerConfiguration>, pub language_servers: Vec<LanguageServerFeatureConfiguration>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub indent: Option<IndentationConfiguration>, pub indent: Option<IndentationConfiguration>,
@ -208,6 +211,68 @@ impl<'de> Deserialize<'de> for FileType {
} }
} }
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum LanguageServerFeature {
Format,
GotoDeclaration,
GotoDefinition,
GotoTypeDefinition,
GotoReference,
GotoImplementation,
// Goto, use bitflags, combining previous Goto members?
SignatureHelp,
Hover,
DocumentHighlight,
Completion,
CodeAction,
WorkspaceCommand,
DocumentSymbols,
WorkspaceSymbols,
// Symbols, use bitflags, see above?
Diagnostics,
RenameSymbol,
InlayHints,
}
impl Display for LanguageServerFeature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LanguageServerFeature::Format => write!(f, "format"),
LanguageServerFeature::GotoDeclaration => write!(f, "goto-declaration"),
LanguageServerFeature::GotoDefinition => write!(f, "goto-definition"),
LanguageServerFeature::GotoTypeDefinition => write!(f, "goto-type-definition"),
LanguageServerFeature::GotoReference => write!(f, "goto-type-definition"),
LanguageServerFeature::GotoImplementation => write!(f, "goto-implementation"),
LanguageServerFeature::SignatureHelp => write!(f, "signature-help"),
LanguageServerFeature::Hover => write!(f, "hover"),
LanguageServerFeature::DocumentHighlight => write!(f, "document-highlight"),
LanguageServerFeature::Completion => write!(f, "completion"),
LanguageServerFeature::CodeAction => write!(f, "code-action"),
LanguageServerFeature::WorkspaceCommand => write!(f, "workspace-command"),
LanguageServerFeature::DocumentSymbols => write!(f, "document-symbols"),
LanguageServerFeature::WorkspaceSymbols => write!(f, "workspace-symbols"),
LanguageServerFeature::Diagnostics => write!(f, "diagnostics"),
LanguageServerFeature::RenameSymbol => write!(f, "rename-symbol"),
LanguageServerFeature::InlayHints => write!(f, "inlay-hints"),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged, rename_all = "kebab-case", deny_unknown_fields)]
pub enum LanguageServerFeatureConfiguration {
#[serde(rename_all = "kebab-case")]
Features {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
only_features: Vec<LanguageServerFeature>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
except_features: Vec<LanguageServerFeature>,
name: String,
},
Simple(String),
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct LanguageServerConfiguration { pub struct LanguageServerConfiguration {
@ -217,9 +282,10 @@ pub struct LanguageServerConfiguration {
pub args: Vec<String>, pub args: Vec<String>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")] #[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub environment: HashMap<String, String>, pub environment: HashMap<String, String>,
#[serde(default, skip_serializing, deserialize_with = "deserialize_lsp_config")]
pub config: Option<serde_json::Value>,
#[serde(default = "default_timeout")] #[serde(default = "default_timeout")]
pub timeout: u64, pub timeout: u64,
pub language_id: Option<String>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -584,6 +650,15 @@ pub struct SoftWrap {
pub wrap_at_text_width: Option<bool>, pub wrap_at_text_width: Option<bool>,
} }
impl LanguageServerFeatureConfiguration {
pub fn name(&self) -> &String {
match self {
LanguageServerFeatureConfiguration::Simple(name) => name,
LanguageServerFeatureConfiguration::Features { name, .. } => name,
}
}
}
// Expose loader as Lazy<> global since it's always static? // Expose loader as Lazy<> global since it's always static?
#[derive(Debug)] #[derive(Debug)]
@ -594,6 +669,8 @@ pub struct Loader {
language_config_ids_by_suffix: HashMap<String, usize>, language_config_ids_by_suffix: HashMap<String, usize>,
language_config_ids_by_shebang: HashMap<String, usize>, language_config_ids_by_shebang: HashMap<String, usize>,
language_server_configs: HashMap<String, LanguageServerConfiguration>,
scopes: ArcSwap<Vec<String>>, scopes: ArcSwap<Vec<String>>,
} }
@ -601,6 +678,7 @@ impl Loader {
pub fn new(config: Configuration) -> Self { pub fn new(config: Configuration) -> Self {
let mut loader = Self { let mut loader = Self {
language_configs: Vec::new(), language_configs: Vec::new(),
language_server_configs: config.language_server,
language_config_ids_by_extension: HashMap::new(), language_config_ids_by_extension: HashMap::new(),
language_config_ids_by_suffix: HashMap::new(), language_config_ids_by_suffix: HashMap::new(),
language_config_ids_by_shebang: HashMap::new(), language_config_ids_by_shebang: HashMap::new(),
@ -725,6 +803,10 @@ impl Loader {
self.language_configs.iter() self.language_configs.iter()
} }
pub fn language_server_configs(&self) -> &HashMap<String, LanguageServerConfiguration> {
&self.language_server_configs
}
pub fn set_scopes(&self, scopes: Vec<String>) { pub fn set_scopes(&self, scopes: Vec<String>) {
self.scopes.store(Arc::new(scopes)); self.scopes.store(Arc::new(scopes));
@ -2370,7 +2452,10 @@ mod test {
"#, "#,
); );
let loader = Loader::new(Configuration { language: vec![] }); let loader = Loader::new(Configuration {
language: vec![],
language_server: HashMap::new(),
});
let language = get_language("rust").unwrap(); let language = get_language("rust").unwrap();
let query = Query::new(language, query_str).unwrap(); let query = Query::new(language, query_str).unwrap();
@ -2429,7 +2514,10 @@ mod test {
.map(String::from) .map(String::from)
.collect(); .collect();
let loader = Loader::new(Configuration { language: vec![] }); let loader = Loader::new(Configuration {
language: vec![],
language_server: HashMap::new(),
});
let language = get_language("rust").unwrap(); let language = get_language("rust").unwrap();
let config = HighlightConfiguration::new( let config = HighlightConfiguration::new(
@ -2532,7 +2620,10 @@ mod test {
) { ) {
let source = Rope::from_str(source); let source = Rope::from_str(source);
let loader = Loader::new(Configuration { language: vec![] }); let loader = Loader::new(Configuration {
language: vec![],
language_server: HashMap::new(),
});
let language = get_language(language_name).unwrap(); let language = get_language(language_name).unwrap();
let config = HighlightConfiguration::new(language, "", "", "").unwrap(); let config = HighlightConfiguration::new(language, "", "", "").unwrap();

@ -44,6 +44,7 @@ fn workspace_for_uri(uri: lsp::Url) -> WorkspaceFolder {
#[derive(Debug)] #[derive(Debug)]
pub struct Client { pub struct Client {
id: usize, id: usize,
name: String,
_process: Child, _process: Child,
server_tx: UnboundedSender<Payload>, server_tx: UnboundedSender<Payload>,
request_counter: AtomicU64, request_counter: AtomicU64,
@ -166,8 +167,7 @@ impl Client {
tokio::spawn(self.did_change_workspace(vec![workspace_for_uri(root_uri)], Vec::new())); tokio::spawn(self.did_change_workspace(vec![workspace_for_uri(root_uri)], Vec::new()));
} }
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity, clippy::too_many_arguments)]
#[allow(clippy::too_many_arguments)]
pub fn start( pub fn start(
cmd: &str, cmd: &str,
args: &[String], args: &[String],
@ -176,6 +176,7 @@ impl Client {
root_markers: &[String], root_markers: &[String],
manual_roots: &[PathBuf], manual_roots: &[PathBuf],
id: usize, id: usize,
name: String,
req_timeout: u64, req_timeout: u64,
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> { ) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> {
@ -200,7 +201,7 @@ impl Client {
let stderr = BufReader::new(process.stderr.take().expect("Failed to open stderr")); let stderr = BufReader::new(process.stderr.take().expect("Failed to open stderr"));
let (server_rx, server_tx, initialize_notify) = let (server_rx, server_tx, initialize_notify) =
Transport::start(reader, writer, stderr, id); Transport::start(reader, writer, stderr, id, name.clone());
let (workspace, workspace_is_cwd) = find_workspace(); let (workspace, workspace_is_cwd) = find_workspace();
let workspace = path::get_normalized_path(&workspace); let workspace = path::get_normalized_path(&workspace);
let root = find_lsp_workspace( let root = find_lsp_workspace(
@ -225,6 +226,7 @@ impl Client {
let client = Self { let client = Self {
id, id,
name,
_process: process, _process: process,
server_tx, server_tx,
request_counter: AtomicU64::new(0), request_counter: AtomicU64::new(0),
@ -240,6 +242,10 @@ impl Client {
Ok((client, server_rx, initialize_notify)) Ok((client, server_rx, initialize_notify))
} }
pub fn name(&self) -> &String {
&self.name
}
pub fn id(&self) -> usize { pub fn id(&self) -> usize {
self.id self.id
} }

@ -17,19 +17,16 @@ use helix_core::{
use tokio::sync::mpsc::UnboundedReceiver; use tokio::sync::mpsc::UnboundedReceiver;
use std::{ use std::{
collections::{hash_map::Entry, HashMap}, collections::HashMap,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::{ sync::Arc,
atomic::{AtomicUsize, Ordering},
Arc,
},
}; };
use thiserror::Error; use thiserror::Error;
use tokio_stream::wrappers::UnboundedReceiverStream; use tokio_stream::wrappers::UnboundedReceiverStream;
pub type Result<T> = core::result::Result<T, Error>; pub type Result<T> = core::result::Result<T, Error>;
type LanguageId = String; type LanguageServerName = String;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {
@ -49,7 +46,7 @@ pub enum Error {
Other(#[from] anyhow::Error), Other(#[from] anyhow::Error),
} }
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum OffsetEncoding { pub enum OffsetEncoding {
/// UTF-8 code units aka bytes /// UTF-8 code units aka bytes
Utf8, Utf8,
@ -624,23 +621,18 @@ impl Notification {
#[derive(Debug)] #[derive(Debug)]
pub struct Registry { pub struct Registry {
inner: HashMap<LanguageId, Vec<(usize, Arc<Client>)>>, inner: HashMap<LanguageServerName, Vec<Arc<Client>>>,
syn_loader: Arc<helix_core::syntax::Loader>,
counter: AtomicUsize, counter: usize,
pub incoming: SelectAll<UnboundedReceiverStream<(usize, Call)>>, pub incoming: SelectAll<UnboundedReceiverStream<(usize, Call)>>,
} }
impl Default for Registry {
fn default() -> Self {
Self::new()
}
}
impl Registry { impl Registry {
pub fn new() -> Self { pub fn new(syn_loader: Arc<helix_core::syntax::Loader>) -> Self {
Self { Self {
inner: HashMap::new(), inner: HashMap::new(),
counter: AtomicUsize::new(0), syn_loader,
counter: 0,
incoming: SelectAll::new(), incoming: SelectAll::new(),
} }
} }
@ -649,15 +641,43 @@ impl Registry {
self.inner self.inner
.values() .values()
.flatten() .flatten()
.find(|(client_id, _)| client_id == &id) .find(|client| client.id() == id)
.map(|(_, client)| client.as_ref()) .map(|client| &**client)
} }
pub fn remove_by_id(&mut self, id: usize) { pub fn remove_by_id(&mut self, id: usize) {
self.inner.retain(|_, clients| { self.inner.retain(|_, language_servers| {
clients.retain(|&(client_id, _)| client_id != id); language_servers.retain(|ls| id != ls.id());
!clients.is_empty() !language_servers.is_empty()
}) });
}
fn start_client(
&mut self,
name: String,
ls_config: &LanguageConfiguration,
doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf],
enable_snippets: bool,
) -> Result<Arc<Client>> {
let config = self
.syn_loader
.language_server_configs()
.get(&name)
.ok_or_else(|| anyhow::anyhow!("Language server '{name}' not defined"))?;
self.counter += 1;
let id = self.counter;
let NewClient(client, incoming) = start_client(
id,
name,
ls_config,
config,
doc_path,
root_dirs,
enable_snippets,
)?;
self.incoming.push(UnboundedReceiverStream::new(incoming));
Ok(client)
} }
pub fn restart( pub fn restart(
@ -666,48 +686,46 @@ impl Registry {
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf], root_dirs: &[PathBuf],
enable_snippets: bool, enable_snippets: bool,
) -> Result<Option<Arc<Client>>> { ) -> Result<Vec<Arc<Client>>> {
let config = match &language_config.language_server { language_config
Some(config) => config, .language_servers
None => return Ok(None), .iter()
}; .filter_map(|config| {
let name = config.name().clone();
let scope = language_config.scope.clone();
#[allow(clippy::map_entry)]
match self.inner.entry(scope) { if self.inner.contains_key(&name) {
Entry::Vacant(_) => Ok(None), let client = match self.start_client(
Entry::Occupied(mut entry) => { name.clone(),
// initialize a new client language_config,
let id = self.counter.fetch_add(1, Ordering::Relaxed); doc_path,
root_dirs,
let NewClientResult(client, incoming) = start_client( enable_snippets,
id, ) {
language_config, Ok(client) => client,
config, error => return Some(error),
doc_path, };
root_dirs, let old_clients = self.inner.insert(name, vec![client.clone()]).unwrap();
enable_snippets,
)?;
self.incoming.push(UnboundedReceiverStream::new(incoming));
let old_clients = entry.insert(vec![(id, client.clone())]); // TODO what if there are different language servers for different workspaces,
// I think the language servers will be stopped without being restarted, which is not intended
for old_client in old_clients {
tokio::spawn(async move {
let _ = old_client.force_shutdown().await;
});
}
for (_, old_client) in old_clients { Some(Ok(client))
tokio::spawn(async move { } else {
let _ = old_client.force_shutdown().await; None
});
} }
})
Ok(Some(client)) .collect()
}
}
} }
pub fn stop(&mut self, language_config: &LanguageConfiguration) { pub fn stop(&mut self, name: &str) {
let scope = language_config.scope.clone(); if let Some(clients) = self.inner.remove(name) {
for client in clients {
if let Some(clients) = self.inner.remove(&scope) {
for (_, client) in clients {
tokio::spawn(async move { tokio::spawn(async move {
let _ = client.force_shutdown().await; let _ = client.force_shutdown().await;
}); });
@ -721,37 +739,35 @@ impl Registry {
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf], root_dirs: &[PathBuf],
enable_snippets: bool, enable_snippets: bool,
) -> Result<Option<Arc<Client>>> { ) -> Result<Vec<Arc<Client>>> {
let config = match &language_config.language_server { language_config
Some(config) => config, .language_servers
None => return Ok(None), .iter()
}; .map(|features| {
let name = features.name();
let clients = self.inner.entry(language_config.scope.clone()).or_default(); if let Some(clients) = self.inner.get_mut(name) {
// check if we already have a client for this documents root that we can reuse if let Some((_, client)) = clients.iter_mut().enumerate().find(|(i, client)| {
if let Some((_, client)) = clients.iter_mut().enumerate().find(|(i, (_, client))| { client.try_add_doc(&language_config.roots, root_dirs, doc_path, *i == 0)
client.try_add_doc(&language_config.roots, root_dirs, doc_path, *i == 0) }) {
}) { return Ok(client.clone());
return Ok(Some(client.1.clone())); }
} }
// initialize a new client let client = self.start_client(
let id = self.counter.fetch_add(1, Ordering::Relaxed); name.clone(),
language_config,
let NewClientResult(client, incoming) = start_client( doc_path,
id, root_dirs,
language_config, enable_snippets,
config, )?;
doc_path, let clients = self.inner.entry(features.name().clone()).or_default();
root_dirs, clients.push(client.clone());
enable_snippets, Ok(client)
)?; })
clients.push((id, client.clone())); .collect()
self.incoming.push(UnboundedReceiverStream::new(incoming));
Ok(Some(client))
} }
pub fn iter_clients(&self) -> impl Iterator<Item = &Arc<Client>> { pub fn iter_clients(&self) -> impl Iterator<Item = &Arc<Client>> {
self.inner.values().flatten().map(|(_, client)| client) self.inner.values().flatten()
} }
} }
@ -833,26 +849,28 @@ impl LspProgressMap {
} }
} }
struct NewClientResult(Arc<Client>, UnboundedReceiver<(usize, Call)>); struct NewClient(Arc<Client>, UnboundedReceiver<(usize, Call)>);
/// start_client takes both a LanguageConfiguration and a LanguageServerConfiguration to ensure that /// start_client takes both a LanguageConfiguration and a LanguageServerConfiguration to ensure that
/// it is only called when it makes sense. /// it is only called when it makes sense.
fn start_client( fn start_client(
id: usize, id: usize,
name: String,
config: &LanguageConfiguration, config: &LanguageConfiguration,
ls_config: &LanguageServerConfiguration, ls_config: &LanguageServerConfiguration,
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf], root_dirs: &[PathBuf],
enable_snippets: bool, enable_snippets: bool,
) -> Result<NewClientResult> { ) -> Result<NewClient> {
let (client, incoming, initialize_notify) = Client::start( let (client, incoming, initialize_notify) = Client::start(
&ls_config.command, &ls_config.command,
&ls_config.args, &ls_config.args,
config.config.clone(), ls_config.config.clone(),
ls_config.environment.clone(), ls_config.environment.clone(),
&config.roots, &config.roots,
config.workspace_lsp_roots.as_deref().unwrap_or(root_dirs), config.workspace_lsp_roots.as_deref().unwrap_or(root_dirs),
id, id,
name,
ls_config.timeout, ls_config.timeout,
doc_path, doc_path,
)?; )?;
@ -886,7 +904,7 @@ fn start_client(
initialize_notify.notify_one(); initialize_notify.notify_one();
}); });
Ok(NewClientResult(client, incoming)) Ok(NewClient(client, incoming))
} }
/// Find an LSP workspace of a file using the following mechanism: /// Find an LSP workspace of a file using the following mechanism:

@ -38,6 +38,7 @@ enum ServerMessage {
#[derive(Debug)] #[derive(Debug)]
pub struct Transport { pub struct Transport {
id: usize, id: usize,
name: String,
pending_requests: Mutex<HashMap<jsonrpc::Id, Sender<Result<Value>>>>, pending_requests: Mutex<HashMap<jsonrpc::Id, Sender<Result<Value>>>>,
} }
@ -47,6 +48,7 @@ impl Transport {
server_stdin: BufWriter<ChildStdin>, server_stdin: BufWriter<ChildStdin>,
server_stderr: BufReader<ChildStderr>, server_stderr: BufReader<ChildStderr>,
id: usize, id: usize,
name: String,
) -> ( ) -> (
UnboundedReceiver<(usize, jsonrpc::Call)>, UnboundedReceiver<(usize, jsonrpc::Call)>,
UnboundedSender<Payload>, UnboundedSender<Payload>,
@ -58,6 +60,7 @@ impl Transport {
let transport = Self { let transport = Self {
id, id,
name,
pending_requests: Mutex::new(HashMap::default()), pending_requests: Mutex::new(HashMap::default()),
}; };
@ -83,6 +86,7 @@ impl Transport {
async fn recv_server_message( async fn recv_server_message(
reader: &mut (impl AsyncBufRead + Unpin + Send), reader: &mut (impl AsyncBufRead + Unpin + Send),
buffer: &mut String, buffer: &mut String,
language_server_name: &str,
) -> Result<ServerMessage> { ) -> Result<ServerMessage> {
let mut content_length = None; let mut content_length = None;
loop { loop {
@ -124,7 +128,7 @@ impl Transport {
reader.read_exact(&mut content).await?; reader.read_exact(&mut content).await?;
let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?; let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?;
info!("<- {}", msg); info!("{language_server_name} <- {msg}");
// try parsing as output (server response) or call (server request) // try parsing as output (server response) or call (server request)
let output: serde_json::Result<ServerMessage> = serde_json::from_str(msg); let output: serde_json::Result<ServerMessage> = serde_json::from_str(msg);
@ -135,12 +139,13 @@ impl Transport {
async fn recv_server_error( async fn recv_server_error(
err: &mut (impl AsyncBufRead + Unpin + Send), err: &mut (impl AsyncBufRead + Unpin + Send),
buffer: &mut String, buffer: &mut String,
language_server_name: &str,
) -> Result<()> { ) -> Result<()> {
buffer.truncate(0); buffer.truncate(0);
if err.read_line(buffer).await? == 0 { if err.read_line(buffer).await? == 0 {
return Err(Error::StreamClosed); return Err(Error::StreamClosed);
}; };
error!("err <- {:?}", buffer); error!("{language_server_name} err <- {buffer:?}");
Ok(()) Ok(())
} }
@ -162,15 +167,17 @@ impl Transport {
Payload::Notification(value) => serde_json::to_string(&value)?, Payload::Notification(value) => serde_json::to_string(&value)?,
Payload::Response(error) => serde_json::to_string(&error)?, Payload::Response(error) => serde_json::to_string(&error)?,
}; };
self.send_string_to_server(server_stdin, json).await self.send_string_to_server(server_stdin, json, &self.name)
.await
} }
async fn send_string_to_server( async fn send_string_to_server(
&self, &self,
server_stdin: &mut BufWriter<ChildStdin>, server_stdin: &mut BufWriter<ChildStdin>,
request: String, request: String,
language_server_name: &str,
) -> Result<()> { ) -> Result<()> {
info!("-> {}", request); info!("{language_server_name} -> {request}");
// send the headers // send the headers
server_stdin server_stdin
@ -189,9 +196,13 @@ impl Transport {
&self, &self,
client_tx: &UnboundedSender<(usize, jsonrpc::Call)>, client_tx: &UnboundedSender<(usize, jsonrpc::Call)>,
msg: ServerMessage, msg: ServerMessage,
language_server_name: &str,
) -> Result<()> { ) -> Result<()> {
match msg { match msg {
ServerMessage::Output(output) => self.process_request_response(output).await?, ServerMessage::Output(output) => {
self.process_request_response(output, language_server_name)
.await?
}
ServerMessage::Call(call) => { ServerMessage::Call(call) => {
client_tx client_tx
.send((self.id, call)) .send((self.id, call))
@ -202,14 +213,18 @@ impl Transport {
Ok(()) Ok(())
} }
async fn process_request_response(&self, output: jsonrpc::Output) -> Result<()> { async fn process_request_response(
&self,
output: jsonrpc::Output,
language_server_name: &str,
) -> Result<()> {
let (id, result) = match output { let (id, result) = match output {
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => { jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
info!("<- {}", result); info!("{language_server_name} <- {}", result);
(id, Ok(result)) (id, Ok(result))
} }
jsonrpc::Output::Failure(jsonrpc::Failure { id, error, .. }) => { jsonrpc::Output::Failure(jsonrpc::Failure { id, error, .. }) => {
error!("<- {}", error); error!("{language_server_name} <- {error}");
(id, Err(error.into())) (id, Err(error.into()))
} }
}; };
@ -240,12 +255,17 @@ impl Transport {
) { ) {
let mut recv_buffer = String::new(); let mut recv_buffer = String::new();
loop { loop {
match Self::recv_server_message(&mut server_stdout, &mut recv_buffer).await { match Self::recv_server_message(&mut server_stdout, &mut recv_buffer, &transport.name)
.await
{
Ok(msg) => { Ok(msg) => {
match transport.process_server_message(&client_tx, msg).await { match transport
.process_server_message(&client_tx, msg, &transport.name)
.await
{
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{} err: <- {err:?}", transport.name);
break; break;
} }
}; };
@ -270,7 +290,7 @@ impl Transport {
params: jsonrpc::Params::None, params: jsonrpc::Params::None,
})); }));
match transport match transport
.process_server_message(&client_tx, notification) .process_server_message(&client_tx, notification, &transport.name)
.await .await
{ {
Ok(_) => {} Ok(_) => {}
@ -281,20 +301,22 @@ impl Transport {
break; break;
} }
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{} err: <- {err:?}", transport.name);
break; break;
} }
} }
} }
} }
async fn err(_transport: Arc<Self>, mut server_stderr: BufReader<ChildStderr>) { async fn err(transport: Arc<Self>, mut server_stderr: BufReader<ChildStderr>) {
let mut recv_buffer = String::new(); let mut recv_buffer = String::new();
loop { loop {
match Self::recv_server_error(&mut server_stderr, &mut recv_buffer).await { match Self::recv_server_error(&mut server_stderr, &mut recv_buffer, &transport.name)
.await
{
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{} err: <- {err:?}", transport.name);
break; break;
} }
} }
@ -348,10 +370,11 @@ impl Transport {
method: lsp_types::notification::Initialized::METHOD.to_string(), method: lsp_types::notification::Initialized::METHOD.to_string(),
params: jsonrpc::Params::None, params: jsonrpc::Params::None,
})); }));
match transport.process_server_message(&client_tx, notification).await { let language_server_name = &transport.name;
match transport.process_server_message(&client_tx, notification, language_server_name).await {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{language_server_name} err: <- {err:?}");
} }
} }
@ -361,7 +384,7 @@ impl Transport {
match transport.send_payload_to_server(&mut server_stdin, msg).await { match transport.send_payload_to_server(&mut server_stdin, msg).await {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{language_server_name} err: <- {err:?}");
} }
} }
} }
@ -380,7 +403,7 @@ impl Transport {
match transport.send_payload_to_server(&mut server_stdin, msg).await { match transport.send_payload_to_server(&mut server_stdin, msg).await {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{} err: <- {err:?}", transport.name);
} }
} }
} }

@ -30,6 +30,7 @@ use crate::{
use log::{debug, error, warn}; use log::{debug, error, warn};
use std::{ use std::{
collections::btree_map::Entry,
io::{stdin, stdout}, io::{stdin, stdout},
path::Path, path::Path,
sync::Arc, sync::Arc,
@ -564,7 +565,7 @@ impl Application {
let doc = doc_mut!(self.editor, &doc_save_event.doc_id); let doc = doc_mut!(self.editor, &doc_save_event.doc_id);
let id = doc.id(); let id = doc.id();
doc.detect_language(loader); doc.detect_language(loader);
let _ = self.editor.refresh_language_server(id); self.editor.refresh_language_servers(id);
} }
// TODO: fix being overwritten by lsp // TODO: fix being overwritten by lsp
@ -662,6 +663,18 @@ impl Application {
) { ) {
use helix_lsp::{Call, MethodCall, Notification}; use helix_lsp::{Call, MethodCall, Notification};
macro_rules! language_server {
() => {
match self.editor.language_servers.get_by_id(server_id) {
Some(language_server) => language_server,
None => {
warn!("can't find language server with id `{}`", server_id);
return;
}
}
};
}
match call { match call {
Call::Notification(helix_lsp::jsonrpc::Notification { method, params, .. }) => { Call::Notification(helix_lsp::jsonrpc::Notification { method, params, .. }) => {
let notification = match Notification::parse(&method, params) { let notification = match Notification::parse(&method, params) {
@ -677,14 +690,7 @@ impl Application {
match notification { match notification {
Notification::Initialized => { Notification::Initialized => {
let language_server = let language_server = language_server!();
match self.editor.language_servers.get_by_id(server_id) {
Some(language_server) => language_server,
None => {
warn!("can't find language server with id `{}`", server_id);
return;
}
};
// Trigger a workspace/didChangeConfiguration notification after initialization. // Trigger a workspace/didChangeConfiguration notification after initialization.
// This might not be required by the spec but Neovim does this as well, so it's // This might not be required by the spec but Neovim does this as well, so it's
@ -694,7 +700,7 @@ impl Application {
} }
let docs = self.editor.documents().filter(|doc| { let docs = self.editor.documents().filter(|doc| {
doc.language_server().map(|server| server.id()) == Some(server_id) doc.language_servers().iter().any(|l| l.id() == server_id)
}); });
// trigger textDocument/didOpen for docs that are already open // trigger textDocument/didOpen for docs that are already open
@ -723,6 +729,7 @@ impl Application {
return; return;
} }
}; };
let offset_encoding = language_server!().offset_encoding();
let doc = self.editor.document_by_path_mut(&path).filter(|doc| { let doc = self.editor.document_by_path_mut(&path).filter(|doc| {
if let Some(version) = params.version { if let Some(version) = params.version {
if version != doc.version() { if version != doc.version() {
@ -745,18 +752,11 @@ impl Application {
use helix_core::diagnostic::{Diagnostic, Range, Severity::*}; use helix_core::diagnostic::{Diagnostic, Range, Severity::*};
use lsp::DiagnosticSeverity; use lsp::DiagnosticSeverity;
let language_server = if let Some(language_server) = doc.language_server() {
language_server
} else {
log::warn!("Discarding diagnostic because language server is not initialized: {:?}", diagnostic);
return None;
};
// TODO: convert inside server // TODO: convert inside server
let start = if let Some(start) = lsp_pos_to_pos( let start = if let Some(start) = lsp_pos_to_pos(
text, text,
diagnostic.range.start, diagnostic.range.start,
language_server.offset_encoding(), offset_encoding,
) { ) {
start start
} else { } else {
@ -764,11 +764,9 @@ impl Application {
return None; return None;
}; };
let end = if let Some(end) = lsp_pos_to_pos( let end = if let Some(end) =
text, lsp_pos_to_pos(text, diagnostic.range.end, offset_encoding)
diagnostic.range.end, {
language_server.offset_encoding(),
) {
end end
} else { } else {
log::warn!("lsp position out of bounds - {:?}", diagnostic); log::warn!("lsp position out of bounds - {:?}", diagnostic);
@ -807,14 +805,19 @@ impl Application {
None => None, None => None,
}; };
let tags = if let Some(ref tags) = diagnostic.tags { let tags = if let Some(tags) = &diagnostic.tags {
let new_tags = tags.iter().filter_map(|tag| { let new_tags = tags
match *tag { .iter()
lsp::DiagnosticTag::DEPRECATED => Some(DiagnosticTag::Deprecated), .filter_map(|tag| match *tag {
lsp::DiagnosticTag::UNNECESSARY => Some(DiagnosticTag::Unnecessary), lsp::DiagnosticTag::DEPRECATED => {
_ => None Some(DiagnosticTag::Deprecated)
} }
}).collect(); lsp::DiagnosticTag::UNNECESSARY => {
Some(DiagnosticTag::Unnecessary)
}
_ => None,
})
.collect();
new_tags new_tags
} else { } else {
@ -830,11 +833,12 @@ impl Application {
tags, tags,
source: diagnostic.source.clone(), source: diagnostic.source.clone(),
data: diagnostic.data.clone(), data: diagnostic.data.clone(),
language_server_id: server_id,
}) })
}) })
.collect(); .collect();
doc.set_diagnostics(diagnostics); doc.replace_diagnostics(diagnostics, server_id);
} }
// Sort diagnostics first by severity and then by line numbers. // Sort diagnostics first by severity and then by line numbers.
@ -842,13 +846,26 @@ impl Application {
params params
.diagnostics .diagnostics
.sort_unstable_by_key(|d| (d.severity, d.range.start)); .sort_unstable_by_key(|d| (d.severity, d.range.start));
let diagnostics = params
.diagnostics
.into_iter()
.map(|d| (d, server_id, offset_encoding))
.collect();
// Insert the original lsp::Diagnostics here because we may have no open document // Insert the original lsp::Diagnostics here because we may have no open document
// for diagnosic message and so we can't calculate the exact position. // for diagnosic message and so we can't calculate the exact position.
// When using them later in the diagnostics picker, we calculate them on-demand. // When using them later in the diagnostics picker, we calculate them on-demand.
self.editor match self.editor.diagnostics.entry(params.uri) {
.diagnostics Entry::Occupied(o) => {
.insert(params.uri, params.diagnostics); let current_diagnostics = o.into_mut();
// there may entries of other language servers, which is why we can't overwrite the whole entry
current_diagnostics.retain(|(_, lsp_id, _)| *lsp_id != server_id);
current_diagnostics.extend(diagnostics);
}
Entry::Vacant(v) => {
v.insert(diagnostics);
}
};
} }
Notification::ShowMessage(params) => { Notification::ShowMessage(params) => {
log::warn!("unhandled window/showMessage: {:?}", params); log::warn!("unhandled window/showMessage: {:?}", params);
@ -950,10 +967,12 @@ impl Application {
.editor .editor
.documents_mut() .documents_mut()
.filter_map(|doc| { .filter_map(|doc| {
if doc.language_server().map(|server| server.id()) if doc
== Some(server_id) .language_servers()
.iter()
.any(|server| server.id() == server_id)
{ {
doc.set_diagnostics(Vec::new()); doc.clear_diagnostics(server_id);
doc.url() doc.url()
} else { } else {
None None
@ -1029,28 +1048,15 @@ impl Application {
})) }))
} }
Ok(MethodCall::WorkspaceFolders) => { Ok(MethodCall::WorkspaceFolders) => {
let language_server = Ok(json!(&*language_server!().workspace_folders().await))
self.editor.language_servers.get_by_id(server_id).unwrap();
Ok(json!(&*language_server.workspace_folders().await))
} }
Ok(MethodCall::WorkspaceConfiguration(params)) => { Ok(MethodCall::WorkspaceConfiguration(params)) => {
let language_server = language_server!();
let result: Vec<_> = params let result: Vec<_> = params
.items .items
.iter() .iter()
.map(|item| { .filter_map(|item| {
let mut config = match &item.scope_uri { let mut config = language_server.config()?;
Some(scope) => {
let path = scope.to_file_path().ok()?;
let doc = self.editor.document_by_path(path)?;
doc.language_config()?.config.as_ref()?
}
None => self
.editor
.language_servers
.get_by_id(server_id)?
.config()?,
};
if let Some(section) = item.section.as_ref() { if let Some(section) = item.section.as_ref() {
for part in section.split('.') { for part in section.split('.') {
config = config.get(part)?; config = config.get(part)?;
@ -1074,15 +1080,7 @@ impl Application {
} }
}; };
let language_server = match self.editor.language_servers.get_by_id(server_id) { tokio::spawn(language_server!().reply(id, reply));
Some(language_server) => language_server,
None => {
warn!("can't find language server with id `{}`", server_id);
return;
}
};
tokio::spawn(language_server.reply(id, reply));
} }
Call::Invalid { id } => log::error!("LSP invalid method call id={:?}", id), Call::Invalid { id } => log::error!("LSP invalid method call id={:?}", id),
} }

@ -23,6 +23,7 @@ use helix_core::{
regex::{self, Regex, RegexBuilder}, regex::{self, Regex, RegexBuilder},
search::{self, CharMatcher}, search::{self, CharMatcher},
selection, shellwords, surround, selection, shellwords, surround,
syntax::LanguageServerFeature,
text_annotations::TextAnnotations, text_annotations::TextAnnotations,
textobject, textobject,
tree_sitter::Node, tree_sitter::Node,
@ -54,13 +55,13 @@ use crate::{
job::Callback, job::Callback,
keymap::ReverseKeymap, keymap::ReverseKeymap,
ui::{ ui::{
self, editor::InsertEvent, lsp::SignatureHelp, overlay::overlaid, FilePicker, Picker, self, editor::InsertEvent, lsp::SignatureHelp, overlay::overlaid, CompletionItem,
Popup, Prompt, PromptEvent, FilePicker, Picker, Popup, Prompt, PromptEvent,
}, },
}; };
use crate::job::{self, Jobs}; use crate::job::{self, Jobs};
use futures_util::StreamExt; use futures_util::{stream::FuturesUnordered, StreamExt, TryStreamExt};
use std::{collections::HashMap, fmt, future::Future}; use std::{collections::HashMap, fmt, future::Future};
use std::{collections::HashSet, num::NonZeroUsize}; use std::{collections::HashSet, num::NonZeroUsize};
@ -3029,7 +3030,7 @@ fn exit_select_mode(cx: &mut Context) {
fn goto_first_diag(cx: &mut Context) { fn goto_first_diag(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = match doc.diagnostics().first() { let selection = match doc.shown_diagnostics().next() {
Some(diag) => Selection::single(diag.range.start, diag.range.end), Some(diag) => Selection::single(diag.range.start, diag.range.end),
None => return, None => return,
}; };
@ -3038,7 +3039,7 @@ fn goto_first_diag(cx: &mut Context) {
fn goto_last_diag(cx: &mut Context) { fn goto_last_diag(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = match doc.diagnostics().last() { let selection = match doc.shown_diagnostics().last() {
Some(diag) => Selection::single(diag.range.start, diag.range.end), Some(diag) => Selection::single(diag.range.start, diag.range.end),
None => return, None => return,
}; };
@ -3054,10 +3055,9 @@ fn goto_next_diag(cx: &mut Context) {
.cursor(doc.text().slice(..)); .cursor(doc.text().slice(..));
let diag = doc let diag = doc
.diagnostics() .shown_diagnostics()
.iter()
.find(|diag| diag.range.start > cursor_pos) .find(|diag| diag.range.start > cursor_pos)
.or_else(|| doc.diagnostics().first()); .or_else(|| doc.shown_diagnostics().next());
let selection = match diag { let selection = match diag {
Some(diag) => Selection::single(diag.range.start, diag.range.end), Some(diag) => Selection::single(diag.range.start, diag.range.end),
@ -3075,11 +3075,12 @@ fn goto_prev_diag(cx: &mut Context) {
.cursor(doc.text().slice(..)); .cursor(doc.text().slice(..));
let diag = doc let diag = doc
.diagnostics() .shown_diagnostics()
.iter() .collect::<Vec<_>>()
.into_iter()
.rev() .rev()
.find(|diag| diag.range.start < cursor_pos) .find(|diag| diag.range.start < cursor_pos)
.or_else(|| doc.diagnostics().last()); .or_else(|| doc.shown_diagnostics().last());
let selection = match diag { let selection = match diag {
// NOTE: the selection is reversed because we're jumping to the // NOTE: the selection is reversed because we're jumping to the
@ -3234,60 +3235,72 @@ pub mod insert {
use helix_lsp::lsp; use helix_lsp::lsp;
// if ch matches completion char, trigger completion // if ch matches completion char, trigger completion
let doc = doc_mut!(cx.editor); let doc = doc_mut!(cx.editor);
let language_server = match doc.language_server() { let trigger_completion = doc
Some(language_server) => language_server, .language_servers_with_feature(LanguageServerFeature::Completion)
None => return, .iter()
}; .any(|ls| {
let capabilities = ls.capabilities();
let capabilities = language_server.capabilities(); // TODO: what if trigger is multiple chars long
matches!(&capabilities.completion_provider, Some(lsp::CompletionOptions {
trigger_characters: Some(triggers),
..
}) if triggers.iter().any(|trigger| trigger.contains(ch)))
});
if let Some(lsp::CompletionOptions { if trigger_completion {
trigger_characters: Some(triggers), cx.editor.clear_idle_timer();
.. super::completion(cx);
}) = &capabilities.completion_provider
{
// TODO: what if trigger is multiple chars long
if triggers.iter().any(|trigger| trigger.contains(ch)) {
cx.editor.clear_idle_timer();
super::completion(cx);
}
} }
} }
fn signature_help(cx: &mut Context, ch: char) { fn signature_help(cx: &mut Context, ch: char) {
use futures_util::FutureExt;
use helix_lsp::lsp; use helix_lsp::lsp;
// if ch matches signature_help char, trigger // if ch matches signature_help char, trigger
let doc = doc_mut!(cx.editor); let (view, doc) = current!(cx.editor);
// The language_server!() macro is not used here since it will // lsp doesn't tell us when to close the signature help, so we request
// print an "LSP not active for current buffer" message on // the help information again after common close triggers which should
// every keypress. // return None, which in turn closes the popup.
let language_server = match doc.language_server() { let close_triggers = &[')', ';', '.'];
Some(language_server) => language_server, // TODO support multiple language servers (not just the first that is found)
None => return, let future = doc
}; .language_servers_with_feature(LanguageServerFeature::SignatureHelp)
.iter()
let capabilities = language_server.capabilities(); .find_map(|ls| {
let capabilities = ls.capabilities();
match capabilities {
lsp::ServerCapabilities {
signature_help_provider:
Some(lsp::SignatureHelpOptions {
trigger_characters: Some(triggers),
// TODO: retrigger_characters
..
}),
..
} if triggers.iter().any(|trigger| trigger.contains(ch))
|| close_triggers.contains(&ch) =>
{
let pos = doc.position(view.id, ls.offset_encoding());
ls.text_document_signature_help(doc.identifier(), pos, None)
}
_ if close_triggers.contains(&ch) => ls.text_document_signature_help(
doc.identifier(),
doc.position(view.id, ls.offset_encoding()),
None,
),
// TODO: what if trigger is multiple chars long
_ => None,
}
});
if let lsp::ServerCapabilities { if let Some(future) = future {
signature_help_provider: super::signature_help_impl_with_future(
Some(lsp::SignatureHelpOptions { cx,
trigger_characters: Some(triggers), future.boxed(),
// TODO: retrigger_characters SignatureHelpInvoked::Automatic,
.. )
}),
..
} = capabilities
{
// TODO: what if trigger is multiple chars long
let is_trigger = triggers.iter().any(|trigger| trigger.contains(ch));
// lsp doesn't tell us when to close the signature help, so we request
// the help information again after common close triggers which should
// return None, which in turn closes the popup.
let close_triggers = &[')', ';', '.'];
if is_trigger || close_triggers.contains(&ch) {
super::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
}
} }
} }
@ -3301,7 +3314,7 @@ pub mod insert {
Some(transaction) Some(transaction)
} }
use helix_core::auto_pairs; use helix_core::{auto_pairs, syntax::LanguageServerFeature};
pub fn insert_char(cx: &mut Context, c: char) { pub fn insert_char(cx: &mut Context, c: char) {
let (view, doc) = current_ref!(cx.editor); let (view, doc) = current_ref!(cx.editor);
@ -4046,55 +4059,55 @@ fn format_selections(cx: &mut Context) {
use helix_lsp::{lsp, util::range_to_lsp_range}; use helix_lsp::{lsp, util::range_to_lsp_range};
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let view_id = view.id;
// via lsp if available // via lsp if available
// TODO: else via tree-sitter indentation calculations // TODO: else via tree-sitter indentation calculations
let language_server = match doc.language_server() { if doc.selection(view_id).len() != 1 {
Some(language_server) => language_server,
None => return,
};
let ranges: Vec<lsp::Range> = doc
.selection(view.id)
.iter()
.map(|range| range_to_lsp_range(doc.text(), *range, language_server.offset_encoding()))
.collect();
if ranges.len() != 1 {
cx.editor cx.editor
.set_error("format_selections only supports a single selection for now"); .set_error("format_selections only supports a single selection for now");
return; return;
} }
// TODO: handle fails let (future, offset_encoding) = match doc
// TODO: concurrent map over all ranges .language_servers_with_feature(LanguageServerFeature::Format)
.iter()
let range = ranges[0]; .find_map(|language_server| {
let offset_encoding = language_server.offset_encoding();
let request = match language_server.text_document_range_formatting( let ranges: Vec<lsp::Range> = doc
doc.identifier(), .selection(view_id)
range, .iter()
lsp::FormattingOptions::default(), .map(|range| range_to_lsp_range(doc.text(), *range, offset_encoding))
None, .collect();
) {
Some(future) => future, // TODO: handle fails
// TODO: concurrent map over all ranges
let range = ranges[0];
let future = language_server.text_document_range_formatting(
doc.identifier(),
range,
lsp::FormattingOptions::default(),
None,
)?;
Some((future, offset_encoding))
}) {
Some(future_offset_encoding) => future_offset_encoding,
None => { None => {
cx.editor cx.editor
.set_error("Language server does not support range formatting"); .set_error("No language server supports range formatting");
return; return;
} }
}; };
let edits = tokio::task::block_in_place(|| helix_lsp::block_on(request)).unwrap_or_default(); let edits = tokio::task::block_in_place(|| helix_lsp::block_on(future)).unwrap_or_default();
let transaction = helix_lsp::util::generate_transaction_from_edits( let transaction =
doc.text(), helix_lsp::util::generate_transaction_from_edits(doc.text(), edits, offset_encoding);
edits,
language_server.offset_encoding(),
);
doc.apply(&transaction, view.id); doc.apply(&transaction, view_id);
} }
fn join_selections_impl(cx: &mut Context, select_space: bool) { fn join_selections_impl(cx: &mut Context, select_space: bool) {
@ -4231,21 +4244,45 @@ pub fn completion(cx: &mut Context) {
doc.savepoint(view) doc.savepoint(view)
}; };
let language_server = match doc.language_server() {
Some(language_server) => language_server,
None => return,
};
let offset_encoding = language_server.offset_encoding();
let text = savepoint.text.clone(); let text = savepoint.text.clone();
let cursor = savepoint.cursor(); let cursor = savepoint.cursor();
let pos = pos_to_lsp_pos(&text, cursor, offset_encoding); let mut futures: FuturesUnordered<_> = doc
.language_servers_with_feature(LanguageServerFeature::Completion)
.iter()
// TODO this should probably already been filtered in something like "language_servers_with_feature"
.filter_map(|language_server| {
let language_server_id = language_server.id();
let offset_encoding = language_server.offset_encoding();
let pos = pos_to_lsp_pos(doc.text(), cursor, helix_lsp::OffsetEncoding::Utf8);
let completion_request = language_server.completion(doc.identifier(), pos, None)?;
Some(async move {
let json = completion_request.await?;
let response: Option<lsp::CompletionResponse> = serde_json::from_value(json)?;
let items = match response {
Some(lsp::CompletionResponse::Array(items)) => items,
// TODO: do something with is_incomplete
Some(lsp::CompletionResponse::List(lsp::CompletionList {
is_incomplete: _is_incomplete,
items,
})) => items,
None => Vec::new(),
}
.into_iter()
.map(|item| CompletionItem {
item,
language_server_id,
offset_encoding,
resolved: false,
})
.collect();
let future = match language_server.completion(doc.identifier(), pos, None) { anyhow::Ok(items)
Some(future) => future, })
None => return, })
}; .collect();
// setup a channel that allows the request to be canceled // setup a channel that allows the request to be canceled
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
@ -4254,12 +4291,20 @@ pub fn completion(cx: &mut Context) {
// and the associated request is automatically dropped // and the associated request is automatically dropped
cx.editor.completion_request_handle = Some(tx); cx.editor.completion_request_handle = Some(tx);
let future = async move { let future = async move {
let items_future = async move {
let mut items = Vec::new();
// TODO if one completion request errors, all other completion requests are discarded (even if they're valid)
while let Some(mut lsp_items) = futures.try_next().await? {
items.append(&mut lsp_items);
}
anyhow::Ok(items)
};
tokio::select! { tokio::select! {
biased; biased;
_ = rx => { _ = rx => {
Ok(serde_json::Value::Null) Ok(Vec::new())
} }
res = future => { res = items_future => {
res res
} }
} }
@ -4293,9 +4338,9 @@ pub fn completion(cx: &mut Context) {
}, },
)); ));
cx.callback( cx.jobs.callback(async move {
future, let items = future.await?;
move |editor, compositor, response: Option<lsp::CompletionResponse>| { let call = move |editor: &mut Editor, compositor: &mut Compositor| {
let (view, doc) = current_ref!(editor); let (view, doc) = current_ref!(editor);
// check if the completion request is stale. // check if the completion request is stale.
// //
@ -4306,16 +4351,6 @@ pub fn completion(cx: &mut Context) {
return; return;
} }
let items = match response {
Some(lsp::CompletionResponse::Array(items)) => items,
// TODO: do something with is_incomplete
Some(lsp::CompletionResponse::List(lsp::CompletionList {
is_incomplete: _is_incomplete,
items,
})) => items,
None => Vec::new(),
};
if items.is_empty() { if items.is_empty() {
// editor.set_error("No completion available"); // editor.set_error("No completion available");
return; return;
@ -4326,7 +4361,6 @@ pub fn completion(cx: &mut Context) {
editor, editor,
savepoint, savepoint,
items, items,
offset_encoding,
start_offset, start_offset,
trigger_offset, trigger_offset,
size, size,
@ -4340,8 +4374,9 @@ pub fn completion(cx: &mut Context) {
{ {
compositor.remove(SignatureHelp::ID); compositor.remove(SignatureHelp::ID);
} }
}, };
); Ok(Callback::EditorCompositor(Box::new(call)))
});
} }
// comments // comments
@ -5141,7 +5176,7 @@ async fn shell_impl_async(
helix_view::document::to_writer(&mut stdin, (encoding::UTF_8, false), &input) helix_view::document::to_writer(&mut stdin, (encoding::UTF_8, false), &input)
.await?; .await?;
} }
Ok::<_, anyhow::Error>(()) anyhow::Ok(())
}); });
let (output, _) = tokio::join! { let (output, _) = tokio::join! {
process.wait_with_output(), process.wait_with_output(),

File diff suppressed because it is too large Load Diff

@ -1329,23 +1329,20 @@ fn lsp_workspace_command(
if event != PromptEvent::Validate { if event != PromptEvent::Validate {
return Ok(()); return Ok(());
} }
let doc = doc!(cx.editor);
let (_, doc) = current!(cx.editor); let language_servers =
doc.language_servers_with_feature(LanguageServerFeature::WorkspaceCommand);
let language_server = match doc.language_server() { let (language_server_id, options) = match language_servers.iter().find_map(|ls| {
Some(language_server) => language_server, ls.capabilities()
None => { .execute_command_provider
cx.editor .as_ref()
.set_status("Language server not active for current buffer"); .map(|options| (ls.id(), options))
return Ok(()); }) {
} Some(id_options) => id_options,
};
let options = match &language_server.capabilities().execute_command_provider {
Some(options) => options,
None => { None => {
cx.editor cx.editor.set_status(
.set_status("Workspace commands are not supported for this language server"); "No active language servers for this document support workspace commands",
);
return Ok(()); return Ok(());
} }
}; };
@ -1362,8 +1359,8 @@ fn lsp_workspace_command(
let callback = async move { let callback = async move {
let call: job::Callback = Callback::EditorCompositor(Box::new( let call: job::Callback = Callback::EditorCompositor(Box::new(
move |_editor: &mut Editor, compositor: &mut Compositor| { move |_editor: &mut Editor, compositor: &mut Compositor| {
let picker = ui::Picker::new(commands, (), |cx, command, _action| { let picker = ui::Picker::new(commands, (), move |cx, command, _action| {
execute_lsp_command(cx.editor, command.clone()); execute_lsp_command(cx.editor, language_server_id, command.clone());
}); });
compositor.push(Box::new(overlaid(picker))) compositor.push(Box::new(overlaid(picker)))
}, },
@ -1376,6 +1373,7 @@ fn lsp_workspace_command(
if options.commands.iter().any(|c| c == &command) { if options.commands.iter().any(|c| c == &command) {
execute_lsp_command( execute_lsp_command(
cx.editor, cx.editor,
language_server_id,
helix_lsp::lsp::Command { helix_lsp::lsp::Command {
title: command.clone(), title: command.clone(),
arguments: None, arguments: None,
@ -1426,7 +1424,7 @@ fn lsp_restart(
.collect(); .collect();
for document_id in document_ids_to_refresh { for document_id in document_ids_to_refresh {
cx.editor.refresh_language_server(document_id); cx.editor.refresh_language_servers(document_id);
} }
Ok(()) Ok(())
@ -1443,21 +1441,63 @@ fn lsp_stop(
let doc = doc!(cx.editor); let doc = doc!(cx.editor);
let ls_id = doc // TODO this stops language servers which may be used in another doc/language type that uses the same language servers
.language_server() // I'm not sure if this is really what we want
.map(|ls| ls.id()) let ls_shutdown_names = doc
.context("LSP not running for the current document")?; .language_servers()
.iter()
.map(|ls| ls.name())
.collect::<Vec<_>>();
let config = doc for ls_name in &ls_shutdown_names {
.language_config() cx.editor.language_servers.stop(ls_name);
.context("LSP not defined for the current document")?; }
cx.editor.language_servers.stop(config);
let doc_ids_active_clients: Vec<_> = cx
.editor
.documents()
.filter_map(|doc| {
let doc_active_ls_ids: Vec<_> = doc
.language_servers()
.iter()
.filter(|ls| !ls_shutdown_names.contains(&ls.name()))
.map(|ls| ls.id())
.collect();
let active_clients: Vec<_> = cx
.editor
.language_servers
.iter_clients()
.filter(|client| doc_active_ls_ids.contains(&client.id()))
.map(Clone::clone)
.collect();
if active_clients.len() != doc.language_servers().len() {
Some((doc.id(), active_clients))
} else {
None
}
})
.collect();
for (doc_id, active_clients) in doc_ids_active_clients {
let doc = cx.editor.documents.get_mut(&doc_id).unwrap();
let stopped_clients: Vec<_> = doc
.language_servers()
.iter()
.filter(|ls| {
!active_clients
.iter()
.any(|active_ls| active_ls.id() == ls.id())
})
.map(|ls| ls.id())
.collect(); // is necessary because of borrow-checking
for doc in cx.editor.documents_mut() { for client_id in stopped_clients {
if doc.language_server().map_or(false, |ls| ls.id() == ls_id) { doc.clear_diagnostics(client_id)
doc.set_language_server(None);
doc.set_diagnostics(Default::default());
} }
doc.set_language_servers(active_clients);
} }
Ok(()) Ok(())
@ -1850,7 +1890,7 @@ fn language(
doc.detect_indent_and_line_ending(); doc.detect_indent_and_line_ending();
let id = doc.id(); let id = doc.id();
cx.editor.refresh_language_server(id); cx.editor.refresh_language_servers(id);
Ok(()) Ok(())
} }
@ -2588,7 +2628,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand { TypableCommand {
name: "lsp-restart", name: "lsp-restart",
aliases: &[], aliases: &[],
doc: "Restarts the Language Server that is in use by the current doc", doc: "Restarts the language servers used by the current doc",
fun: lsp_restart, fun: lsp_restart,
signature: CommandSignature::none(), signature: CommandSignature::none(),
}, },

@ -2,7 +2,10 @@ use crossterm::{
style::{Color, Print, Stylize}, style::{Color, Print, Stylize},
tty::IsTty, tty::IsTty,
}; };
use helix_core::config::{default_syntax_loader, user_syntax_loader}; use helix_core::{
config::{default_syntax_loader, user_syntax_loader},
syntax::LanguageServerFeatureConfiguration,
};
use helix_loader::grammar::load_runtime_file; use helix_loader::grammar::load_runtime_file;
use helix_view::clipboard::get_clipboard_provider; use helix_view::clipboard::get_clipboard_provider;
use std::io::Write; use std::io::Write;
@ -192,10 +195,14 @@ pub fn languages_all() -> std::io::Result<()> {
for lang in &syn_loader_conf.language { for lang in &syn_loader_conf.language {
column(&lang.language_id, Color::Reset); column(&lang.language_id, Color::Reset);
let lsp = lang // TODO multiple language servers (check binary for each supported language server, not just the first)
.language_server
.as_ref() let lsp = lang.language_servers.first().and_then(|lsp| {
.map(|lsp| lsp.command.to_string()); syn_loader_conf
.language_server
.get(lsp.name())
.map(|config| config.command.clone())
});
check_binary(lsp); check_binary(lsp);
let dap = lang.debugger.as_ref().map(|dap| dap.command.to_string()); let dap = lang.debugger.as_ref().map(|dap| dap.command.to_string());
@ -264,11 +271,15 @@ pub fn language(lang_str: String) -> std::io::Result<()> {
} }
}; };
// TODO multiple language servers
probe_protocol( probe_protocol(
"language server", "language server",
lang.language_server lang.language_servers.first().and_then(|lsp| {
.as_ref() syn_loader_conf
.map(|lsp| lsp.command.to_string()), .language_server
.get(lsp.name())
.map(|config| config.command.clone())
}),
)?; )?;
probe_protocol( probe_protocol(

@ -15,7 +15,7 @@ use helix_view::{graphics::Rect, Document, Editor};
use crate::commands; use crate::commands;
use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent}; use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};
use helix_lsp::{lsp, util}; use helix_lsp::{lsp, util, OffsetEncoding};
impl menu::Item for CompletionItem { impl menu::Item for CompletionItem {
type Data = (); type Data = ();
@ -38,6 +38,7 @@ impl menu::Item for CompletionItem {
|| self.item.tags.as_ref().map_or(false, |tags| { || self.item.tags.as_ref().map_or(false, |tags| {
tags.contains(&lsp::CompletionItemTag::DEPRECATED) tags.contains(&lsp::CompletionItemTag::DEPRECATED)
}); });
menu::Row::new(vec![ menu::Row::new(vec![
menu::Cell::from(Span::styled( menu::Cell::from(Span::styled(
self.item.label.as_str(), self.item.label.as_str(),
@ -79,19 +80,16 @@ impl menu::Item for CompletionItem {
} }
None => "", None => "",
}), }),
// self.detail.as_deref().unwrap_or("")
// self.label_details
// .as_ref()
// .or(self.detail())
// .as_str(),
]) ])
} }
} }
#[derive(Debug, PartialEq, Default, Clone)] #[derive(Debug, PartialEq, Default, Clone)]
struct CompletionItem { pub struct CompletionItem {
item: lsp::CompletionItem, pub item: lsp::CompletionItem,
resolved: bool, pub language_server_id: usize,
pub offset_encoding: OffsetEncoding,
pub resolved: bool,
} }
/// Wraps a Menu. /// Wraps a Menu.
@ -109,21 +107,13 @@ impl Completion {
pub fn new( pub fn new(
editor: &Editor, editor: &Editor,
savepoint: Arc<SavePoint>, savepoint: Arc<SavePoint>,
mut items: Vec<lsp::CompletionItem>, mut items: Vec<CompletionItem>,
offset_encoding: helix_lsp::OffsetEncoding,
start_offset: usize, start_offset: usize,
trigger_offset: usize, trigger_offset: usize,
) -> Self { ) -> Self {
let replace_mode = editor.config().completion_replace; let replace_mode = editor.config().completion_replace;
// Sort completion items according to their preselect status (given by the LSP server) // Sort completion items according to their preselect status (given by the LSP server)
items.sort_by_key(|item| !item.preselect.unwrap_or(false)); items.sort_by_key(|item| !item.item.preselect.unwrap_or(false));
let items = items
.into_iter()
.map(|item| CompletionItem {
item,
resolved: false,
})
.collect();
// Then create the menu // Then create the menu
let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| { let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| {
@ -131,7 +121,6 @@ impl Completion {
doc: &Document, doc: &Document,
view_id: ViewId, view_id: ViewId,
item: &CompletionItem, item: &CompletionItem,
offset_encoding: helix_lsp::OffsetEncoding,
trigger_offset: usize, trigger_offset: usize,
include_placeholder: bool, include_placeholder: bool,
replace_mode: bool, replace_mode: bool,
@ -154,6 +143,8 @@ impl Completion {
} }
}; };
let offset_encoding = item.offset_encoding;
let Some(range) = util::lsp_range_to_range(doc.text(), edit.range, offset_encoding) else{ let Some(range) = util::lsp_range_to_range(doc.text(), edit.range, offset_encoding) else{
return Transaction::new(doc.text()); return Transaction::new(doc.text());
}; };
@ -247,15 +238,8 @@ impl Completion {
// always present here // always present here
let item = item.unwrap(); let item = item.unwrap();
let transaction = item_to_transaction( let transaction =
doc, item_to_transaction(doc, view.id, item, trigger_offset, true, replace_mode);
view.id,
item,
offset_encoding,
trigger_offset,
true,
replace_mode,
);
doc.apply_temporary(&transaction, view.id); doc.apply_temporary(&transaction, view.id);
} }
PromptEvent::Validate => { PromptEvent::Validate => {
@ -267,10 +251,15 @@ impl Completion {
// always present here // always present here
let mut item = item.unwrap().clone(); let mut item = item.unwrap().clone();
let language_server = editor
.language_servers
.get_by_id(item.language_server_id)
.unwrap();
// resolve item if not yet resolved // resolve item if not yet resolved
if !item.resolved { if !item.resolved {
if let Some(resolved) = if let Some(resolved) =
Self::resolve_completion_item(doc, item.item.clone()) Self::resolve_completion_item(language_server, item.item.clone())
{ {
item.item = resolved; item.item = resolved;
} }
@ -281,7 +270,6 @@ impl Completion {
doc, doc,
view.id, view.id,
&item, &item,
offset_encoding,
trigger_offset, trigger_offset,
false, false,
replace_mode, replace_mode,
@ -299,7 +287,7 @@ impl Completion {
let transaction = util::generate_transaction_from_edits( let transaction = util::generate_transaction_from_edits(
doc.text(), doc.text(),
additional_edits, additional_edits,
offset_encoding, // TODO: should probably transcode in Client item.offset_encoding, // TODO: should probably transcode in Client
); );
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
} }
@ -323,10 +311,17 @@ impl Completion {
} }
fn resolve_completion_item( fn resolve_completion_item(
doc: &Document, language_server: &helix_lsp::Client,
completion_item: lsp::CompletionItem, completion_item: lsp::CompletionItem,
) -> Option<lsp::CompletionItem> { ) -> Option<lsp::CompletionItem> {
let language_server = doc.language_server()?; let completion_resolve_provider = language_server
.capabilities()
.completion_provider
.as_ref()?
.resolve_provider;
if completion_resolve_provider != Some(true) {
return None;
}
let future = language_server.resolve_completion_item(completion_item)?; let future = language_server.resolve_completion_item(completion_item)?;
let response = helix_lsp::block_on(future); let response = helix_lsp::block_on(future);
@ -397,8 +392,11 @@ impl Completion {
Some(item) if !item.resolved => item.clone(), Some(item) if !item.resolved => item.clone(),
_ => return false, _ => return false,
}; };
let language_server = match cx
let language_server = match doc!(cx.editor).language_server() { .editor
.language_servers
.get_by_id(current_item.language_server_id)
{
Some(language_server) => language_server, Some(language_server) => language_server,
None => return false, None => return false,
}; };
@ -422,13 +420,14 @@ impl Completion {
.unwrap() .unwrap()
.completion .completion
{ {
completion.replace_item( let resolved_item = CompletionItem {
current_item, item: resolved_item,
CompletionItem { language_server_id: current_item.language_server_id,
item: resolved_item, offset_encoding: current_item.offset_encoding,
resolved: true, resolved: true,
}, };
);
completion.replace_item(current_item, resolved_item);
} }
}, },
); );

@ -33,7 +33,7 @@ use std::{mem::take, num::NonZeroUsize, path::PathBuf, rc::Rc, sync::Arc};
use tui::{buffer::Buffer as Surface, text::Span}; use tui::{buffer::Buffer as Surface, text::Span};
use super::statusline; use super::{completion::CompletionItem, statusline};
use super::{document::LineDecoration, lsp::SignatureHelp}; use super::{document::LineDecoration, lsp::SignatureHelp};
pub struct EditorView { pub struct EditorView {
@ -650,7 +650,7 @@ impl EditorView {
.primary() .primary()
.cursor(doc.text().slice(..)); .cursor(doc.text().slice(..));
let diagnostics = doc.diagnostics().iter().filter(|diagnostic| { let diagnostics = doc.shown_diagnostics().filter(|diagnostic| {
diagnostic.range.start <= cursor && diagnostic.range.end >= cursor diagnostic.range.start <= cursor && diagnostic.range.end >= cursor
}); });
@ -953,20 +953,13 @@ impl EditorView {
&mut self, &mut self,
editor: &mut Editor, editor: &mut Editor,
savepoint: Arc<SavePoint>, savepoint: Arc<SavePoint>,
items: Vec<helix_lsp::lsp::CompletionItem>, items: Vec<CompletionItem>,
offset_encoding: helix_lsp::OffsetEncoding,
start_offset: usize, start_offset: usize,
trigger_offset: usize, trigger_offset: usize,
size: Rect, size: Rect,
) -> Option<Rect> { ) -> Option<Rect> {
let mut completion = Completion::new( let mut completion =
editor, Completion::new(editor, savepoint, items, start_offset, trigger_offset);
savepoint,
items,
offset_encoding,
start_offset,
trigger_offset,
);
if completion.is_empty() { if completion.is_empty() {
// skip if we got no completion results // skip if we got no completion results

@ -17,7 +17,7 @@ mod text;
use crate::compositor::{Component, Compositor}; use crate::compositor::{Component, Compositor};
use crate::filter_picker_entry; use crate::filter_picker_entry;
use crate::job::{self, Callback}; use crate::job::{self, Callback};
pub use completion::Completion; pub use completion::{Completion, CompletionItem};
pub use editor::EditorView; pub use editor::EditorView;
pub use markdown::Markdown; pub use markdown::Markdown;
pub use menu::Menu; pub use menu::Menu;
@ -238,6 +238,7 @@ pub mod completers {
use crate::ui::prompt::Completion; use crate::ui::prompt::Completion;
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use fuzzy_matcher::FuzzyMatcher; use fuzzy_matcher::FuzzyMatcher;
use helix_core::syntax::LanguageServerFeature;
use helix_view::document::SCRATCH_BUFFER_NAME; use helix_view::document::SCRATCH_BUFFER_NAME;
use helix_view::theme; use helix_view::theme;
use helix_view::{editor::Config, Editor}; use helix_view::{editor::Config, Editor};
@ -393,17 +394,13 @@ pub mod completers {
pub fn lsp_workspace_command(editor: &Editor, input: &str) -> Vec<Completion> { pub fn lsp_workspace_command(editor: &Editor, input: &str) -> Vec<Completion> {
let matcher = Matcher::default(); let matcher = Matcher::default();
let (_, doc) = current_ref!(editor); let language_servers =
doc!(editor).language_servers_with_feature(LanguageServerFeature::WorkspaceCommand);
let language_server = match doc.language_server() { let options = match language_servers
Some(language_server) => language_server, .into_iter()
None => { .find_map(|ls| ls.capabilities().execute_command_provider.as_ref())
return vec![]; {
} Some(id_options) => id_options,
};
let options = match &language_server.capabilities().execute_command_provider {
Some(options) => options,
None => { None => {
return vec![]; return vec![];
} }

@ -197,15 +197,16 @@ where
); );
} }
// TODO think about handling multiple language servers
fn render_lsp_spinner<F>(context: &mut RenderContext, write: F) fn render_lsp_spinner<F>(context: &mut RenderContext, write: F)
where where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy, F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{ {
let language_servers = context.doc.language_servers();
write( write(
context, context,
context language_servers
.doc .first()
.language_server()
.and_then(|srv| { .and_then(|srv| {
context context
.spinners .spinners
@ -225,8 +226,7 @@ where
{ {
let (warnings, errors) = context let (warnings, errors) = context
.doc .doc
.diagnostics() .shown_diagnostics()
.iter()
.fold((0, 0), |mut counts, diag| { .fold((0, 0), |mut counts, diag| {
use helix_core::diagnostic::Severity; use helix_core::diagnostic::Severity;
match diag.severity { match diag.severity {
@ -266,7 +266,7 @@ where
.diagnostics .diagnostics
.values() .values()
.flatten() .flatten()
.fold((0, 0), |mut counts, diag| { .fold((0, 0), |mut counts, (diag, _, _)| {
match diag.severity { match diag.severity {
Some(DiagnosticSeverity::WARNING) => counts.0 += 1, Some(DiagnosticSeverity::WARNING) => counts.0 += 1,
Some(DiagnosticSeverity::ERROR) | None => counts.1 += 1, Some(DiagnosticSeverity::ERROR) | None => counts.1 += 1,

@ -6,7 +6,7 @@ use futures_util::FutureExt;
use helix_core::auto_pairs::AutoPairs; use helix_core::auto_pairs::AutoPairs;
use helix_core::doc_formatter::TextFormat; use helix_core::doc_formatter::TextFormat;
use helix_core::encoding::Encoding; use helix_core::encoding::Encoding;
use helix_core::syntax::Highlight; use helix_core::syntax::{Highlight, LanguageServerFeature, LanguageServerFeatureConfiguration};
use helix_core::text_annotations::{InlineAnnotation, TextAnnotations}; use helix_core::text_annotations::{InlineAnnotation, TextAnnotations};
use helix_core::Range; use helix_core::Range;
use helix_vcs::{DiffHandle, DiffProviderRegistry}; use helix_vcs::{DiffHandle, DiffProviderRegistry};
@ -16,7 +16,7 @@ use serde::de::{self, Deserialize, Deserializer};
use serde::Serialize; use serde::Serialize;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::Cell; use std::cell::Cell;
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use std::fmt::Display; use std::fmt::Display;
use std::future::Future; use std::future::Future;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -180,7 +180,7 @@ pub struct Document {
pub(crate) modified_since_accessed: bool, pub(crate) modified_since_accessed: bool,
diagnostics: Vec<Diagnostic>, diagnostics: Vec<Diagnostic>,
language_server: Option<Arc<helix_lsp::Client>>, language_servers: Vec<Arc<helix_lsp::Client>>,
diff_handle: Option<DiffHandle>, diff_handle: Option<DiffHandle>,
version_control_head: Option<Arc<ArcSwap<Box<str>>>>, version_control_head: Option<Arc<ArcSwap<Box<str>>>>,
@ -616,7 +616,7 @@ impl Document {
last_saved_time: SystemTime::now(), last_saved_time: SystemTime::now(),
last_saved_revision: 0, last_saved_revision: 0,
modified_since_accessed: false, modified_since_accessed: false,
language_server: None, language_servers: Vec::new(),
diff_handle: None, diff_handle: None,
config, config,
version_control_head: None, version_control_head: None,
@ -730,19 +730,24 @@ impl Document {
return Some(formatting_future.boxed()); return Some(formatting_future.boxed());
}; };
let language_server = self.language_server()?;
let text = self.text.clone(); let text = self.text.clone();
let offset_encoding = language_server.offset_encoding(); // finds first language server that supports formatting and then formats
let (offset_encoding, request) = self
let request = language_server.text_document_formatting( .language_servers_with_feature(LanguageServerFeature::Format)
self.identifier(), .iter()
lsp::FormattingOptions { .find_map(|language_server| {
tab_size: self.tab_width() as u32, let offset_encoding = language_server.offset_encoding();
insert_spaces: matches!(self.indent_style, IndentStyle::Spaces(_)), let request = language_server.text_document_formatting(
..Default::default() self.identifier(),
}, lsp::FormattingOptions {
None, tab_size: self.tab_width() as u32,
)?; insert_spaces: matches!(self.indent_style, IndentStyle::Spaces(_)),
..Default::default()
},
None,
)?;
Some((offset_encoding, request))
})?;
let fut = async move { let fut = async move {
let edits = request.await.unwrap_or_else(|e| { let edits = request.await.unwrap_or_else(|e| {
@ -797,13 +802,12 @@ impl Document {
if self.path.is_none() { if self.path.is_none() {
bail!("Can't save with no path set!"); bail!("Can't save with no path set!");
} }
self.path.as_ref().unwrap().clone() self.path.as_ref().unwrap().clone()
} }
}; };
let identifier = self.path().map(|_| self.identifier()); let identifier = self.path().map(|_| self.identifier());
let language_server = self.language_server.clone(); let language_servers = self.language_servers.clone();
// mark changes up to now as saved // mark changes up to now as saved
let current_rev = self.get_current_revision(); let current_rev = self.get_current_revision();
@ -847,14 +851,13 @@ impl Document {
text: text.clone(), text: text.clone(),
}; };
if let Some(language_server) = language_server { for language_server in language_servers {
if !language_server.is_initialized() { if !language_server.is_initialized() {
return Ok(event); return Ok(event);
} }
if let Some(identifier) = &identifier {
if let Some(identifier) = identifier {
if let Some(notification) = if let Some(notification) =
language_server.text_document_did_save(identifier, &text) language_server.text_document_did_save(identifier.clone(), &text)
{ {
notification.await?; notification.await?;
} }
@ -1005,8 +1008,8 @@ impl Document {
} }
/// Set the LSP. /// Set the LSP.
pub fn set_language_server(&mut self, language_server: Option<Arc<helix_lsp::Client>>) { pub fn set_language_servers(&mut self, language_servers: Vec<Arc<helix_lsp::Client>>) {
self.language_server = language_server; self.language_servers = language_servers;
} }
/// Select text within the [`Document`]. /// Select text within the [`Document`].
@ -1159,7 +1162,7 @@ impl Document {
if emit_lsp_notification { if emit_lsp_notification {
// emit lsp notification // emit lsp notification
if let Some(language_server) = self.language_server() { for language_server in self.language_servers() {
let notify = language_server.text_document_did_change( let notify = language_server.text_document_did_change(
self.versioned_identifier(), self.versioned_identifier(),
&old_doc, &old_doc,
@ -1415,18 +1418,13 @@ impl Document {
.map(|language| language.language_id.as_str()) .map(|language| language.language_id.as_str())
} }
/// Language ID for the document. Either the `language-id` from the /// Language ID for the document. Either the `language-id`,
/// `language-server` configuration, or the document language if no /// or the document language name if no `language-id` has been specified.
/// `language-id` has been specified.
pub fn language_id(&self) -> Option<&str> { pub fn language_id(&self) -> Option<&str> {
let language_config = self.language.as_deref()?; self.language_config()?
.language_server_language_id
language_config
.language_server
.as_ref()?
.language_id
.as_deref() .as_deref()
.or(Some(language_config.language_id.as_str())) .or_else(|| self.language_name())
} }
/// Corresponding [`LanguageConfiguration`]. /// Corresponding [`LanguageConfiguration`].
@ -1439,10 +1437,54 @@ impl Document {
self.version self.version
} }
/// Language server if it has been initialized. /// Language servers that have been initialized.
pub fn language_server(&self) -> Option<&helix_lsp::Client> { pub fn language_servers(&self) -> Vec<&helix_lsp::Client> {
let server = self.language_server.as_deref()?; self.language_servers
server.is_initialized().then_some(server) .iter()
.filter_map(|l| if l.is_initialized() { Some(&**l) } else { None })
.collect()
}
// TODO filter also based on LSP capabilities?
pub fn language_servers_with_feature(
&self,
feature: LanguageServerFeature,
) -> Vec<&helix_lsp::Client> {
let language_servers = self.language_servers();
let language_config = match self.language_config() {
Some(language_config) => language_config,
None => return Vec::new(),
};
// O(n^2) but since language_servers will be of very small length,
// I don't see the necessity to optimize
language_config
.language_servers
.iter()
.filter_map(|c| match c {
LanguageServerFeatureConfiguration::Simple(name) => language_servers
.iter()
.find(|ls| ls.name() == name)
.copied(),
LanguageServerFeatureConfiguration::Features {
only_features,
except_features,
name,
} => {
if (only_features.is_empty() || only_features.contains(&feature))
&& !except_features.contains(&feature)
{
language_servers
.iter()
.find(|ls| ls.name() == name)
.copied()
} else {
None
}
}
})
.collect()
} }
pub fn diff_handle(&self) -> Option<&DiffHandle> { pub fn diff_handle(&self) -> Option<&DiffHandle> {
@ -1565,12 +1607,33 @@ impl Document {
&self.diagnostics &self.diagnostics
} }
pub fn set_diagnostics(&mut self, diagnostics: Vec<Diagnostic>) { pub fn shown_diagnostics(&self) -> impl Iterator<Item = &Diagnostic> {
self.diagnostics = diagnostics; let ls_ids: HashSet<_> = self
.language_servers_with_feature(LanguageServerFeature::Diagnostics)
.iter()
.map(|ls| ls.id())
.collect();
self.diagnostics
.iter()
.filter(move |d| ls_ids.contains(&d.language_server_id))
}
pub fn replace_diagnostics(
&mut self,
mut diagnostics: Vec<Diagnostic>,
language_server_id: usize,
) {
self.clear_diagnostics(language_server_id);
self.diagnostics.append(&mut diagnostics);
self.diagnostics self.diagnostics
.sort_unstable_by_key(|diagnostic| diagnostic.range); .sort_unstable_by_key(|diagnostic| diagnostic.range);
} }
pub fn clear_diagnostics(&mut self, language_server_id: usize) {
self.diagnostics
.retain(|d| d.language_server_id != language_server_id);
}
/// Get the document's auto pairs. If the document has a recognized /// Get the document's auto pairs. If the document has a recognized
/// language config with auto pairs configured, returns that; /// language config with auto pairs configured, returns that;
/// otherwise, falls back to the global auto pairs config. If the global /// otherwise, falls back to the global auto pairs config. If the global

@ -48,7 +48,7 @@ use helix_core::{
}; };
use helix_core::{Position, Selection}; use helix_core::{Position, Selection};
use helix_dap as dap; use helix_dap as dap;
use helix_lsp::lsp; use helix_lsp::{lsp, OffsetEncoding};
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}; use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
@ -689,7 +689,7 @@ pub struct WhitespaceCharacters {
impl Default for WhitespaceCharacters { impl Default for WhitespaceCharacters {
fn default() -> Self { fn default() -> Self {
Self { Self {
space: '·', // U+00B7 space: '·', // U+00B7
nbsp: '', // U+237D nbsp: '', // U+237D
tab: '', // U+2192 tab: '', // U+2192
newline: '', // U+23CE newline: '', // U+23CE
@ -818,7 +818,7 @@ pub struct Editor {
pub macro_recording: Option<(char, Vec<KeyEvent>)>, pub macro_recording: Option<(char, Vec<KeyEvent>)>,
pub macro_replaying: Vec<char>, pub macro_replaying: Vec<char>,
pub language_servers: helix_lsp::Registry, pub language_servers: helix_lsp::Registry,
pub diagnostics: BTreeMap<lsp::Url, Vec<lsp::Diagnostic>>, pub diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize, OffsetEncoding)>>,
pub diff_providers: DiffProviderRegistry, pub diff_providers: DiffProviderRegistry,
pub debugger: Option<dap::Client>, pub debugger: Option<dap::Client>,
@ -941,6 +941,7 @@ impl Editor {
syn_loader: Arc<syntax::Loader>, syn_loader: Arc<syntax::Loader>,
config: Arc<dyn DynAccess<Config>>, config: Arc<dyn DynAccess<Config>>,
) -> Self { ) -> Self {
let language_servers = helix_lsp::Registry::new(syn_loader.clone());
let conf = config.load(); let conf = config.load();
let auto_pairs = (&conf.auto_pairs).into(); let auto_pairs = (&conf.auto_pairs).into();
@ -960,7 +961,7 @@ impl Editor {
macro_recording: None, macro_recording: None,
macro_replaying: Vec::new(), macro_replaying: Vec::new(),
theme: theme_loader.default(), theme: theme_loader.default(),
language_servers: helix_lsp::Registry::new(), language_servers,
diagnostics: BTreeMap::new(), diagnostics: BTreeMap::new(),
diff_providers: DiffProviderRegistry::default(), diff_providers: DiffProviderRegistry::default(),
debugger: None, debugger: None,
@ -1093,12 +1094,12 @@ impl Editor {
} }
/// Refreshes the language server for a given document /// Refreshes the language server for a given document
pub fn refresh_language_server(&mut self, doc_id: DocumentId) -> Option<()> { pub fn refresh_language_servers(&mut self, doc_id: DocumentId) -> Option<()> {
self.launch_language_server(doc_id) self.launch_language_servers(doc_id)
} }
/// Launch a language server for a given document /// Launch a language server for a given document
fn launch_language_server(&mut self, doc_id: DocumentId) -> Option<()> { fn launch_language_servers(&mut self, doc_id: DocumentId) -> Option<()> {
if !self.config().lsp.enable { if !self.config().lsp.enable {
return None; return None;
} }
@ -1109,42 +1110,49 @@ impl Editor {
let config = doc.config.load(); let config = doc.config.load();
let root_dirs = &config.workspace_lsp_roots; let root_dirs = &config.workspace_lsp_roots;
// try to find a language server based on the language name // try to find language servers based on the language name
let language_server = lang.as_ref().and_then(|language| { let language_servers = lang.as_ref().and_then(|language| {
self.language_servers self.language_servers
.get(language, path.as_ref(), root_dirs, config.lsp.snippets) .get(language, path.as_ref(), root_dirs, config.lsp.snippets)
.map_err(|e| { .map_err(|e| {
log::error!( log::error!(
"Failed to initialize the LSP for `{}` {{ {} }}", "Failed to initialize the language servers for `{}` {{ {} }}",
language.scope(), language.scope(),
e e
) )
}) })
.ok() .ok()
.flatten()
}); });
let doc = self.document_mut(doc_id)?; let doc = self.document_mut(doc_id)?;
let doc_url = doc.url()?; let doc_url = doc.url()?;
if let Some(language_server) = language_server { if let Some(language_servers) = language_servers {
// only spawn a new lang server if the servers aren't the same // only spawn new lang servers if the servers aren't the same
if Some(language_server.id()) != doc.language_server().map(|server| server.id()) { let doc_language_servers = doc.language_servers();
if let Some(language_server) = doc.language_server() { let spawn_new_servers = language_servers.len() != doc_language_servers.len()
tokio::spawn(language_server.text_document_did_close(doc.identifier())); || language_servers
.iter()
.zip(doc_language_servers.iter())
.any(|(l, dl)| l.id() != dl.id());
if spawn_new_servers {
for doc_language_server in doc_language_servers {
tokio::spawn(doc_language_server.text_document_did_close(doc.identifier()));
} }
let language_id = doc.language_id().map(ToOwned::to_owned).unwrap_or_default(); let language_id = doc.language_id().map(ToOwned::to_owned).unwrap_or_default();
// TODO: this now races with on_init code if the init happens too quickly for language_server in &language_servers {
tokio::spawn(language_server.text_document_did_open( // TODO: this now races with on_init code if the init happens too quickly
doc_url, tokio::spawn(language_server.text_document_did_open(
doc.version(), doc_url.clone(),
doc.text(), doc.version(),
language_id, doc.text(),
)); language_id.clone(),
));
}
doc.set_language_server(Some(language_server)); doc.set_language_servers(language_servers);
} }
} }
Some(()) Some(())
@ -1337,10 +1345,10 @@ impl Editor {
} }
doc.set_version_control_head(self.diff_providers.get_current_head_name(&path)); doc.set_version_control_head(self.diff_providers.get_current_head_name(&path));
let id = self.new_document(doc); let doc_id = self.new_document(doc);
let _ = self.launch_language_server(id); let _ = self.launch_language_servers(doc_id);
id doc_id
}; };
self.switch(id, action); self.switch(id, action);
@ -1368,7 +1376,7 @@ impl Editor {
// This will also disallow any follow-up writes // This will also disallow any follow-up writes
self.saves.remove(&doc_id); self.saves.remove(&doc_id);
if let Some(language_server) = doc.language_server() { for language_server in doc.language_servers() {
// TODO: track error // TODO: track error
tokio::spawn(language_server.text_document_did_close(doc.identifier())); tokio::spawn(language_server.text_document_did_close(doc.identifier()));
} }

@ -55,7 +55,7 @@ pub fn diagnostic<'doc>(
let error = theme.get("error"); let error = theme.get("error");
let info = theme.get("info"); let info = theme.get("info");
let hint = theme.get("hint"); let hint = theme.get("hint");
let diagnostics = doc.diagnostics(); let diagnostics = doc.shown_diagnostics().collect::<Vec<_>>();
Box::new( Box::new(
move |line: usize, _selected: bool, first_visual_line: bool, out: &mut String| { move |line: usize, _selected: bool, first_visual_line: bool, out: &mut String| {

File diff suppressed because it is too large Load Diff

@ -96,11 +96,12 @@ pub fn lang_features() -> Result<String, DynError> {
); );
} }
row.push( row.push(
lc.language_server lc.language_servers
.as_ref() .iter()
.map(|s| s.command.clone()) .filter_map(|ls| config.language_server.get(ls.name()))
.map(|c| md_mono(&c)) .map(|s| md_mono(&s.command.clone()))
.unwrap_or_default(), .collect::<Vec<_>>()
.join(", "),
); );
md.push_str(&md_table_row(&row)); md.push_str(&md_table_row(&row));

Loading…
Cancel
Save