Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supported commands in server capabilities #11850

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions crates/ruff_server/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Scheduling, I/O, and API endpoints.

use std::num::NonZeroUsize;
use std::str::FromStr;

use lsp_server as lsp;
use lsp_types as types;
Expand Down Expand Up @@ -276,6 +277,14 @@ impl Server {
},
},
)),
execute_command_provider: Some(types::ExecuteCommandOptions {
commands: SupportedCommand::all()
.map(|command| command.identifier().to_string())
.to_vec(),
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: Some(false),
},
}),
hover_provider: Some(types::HoverProviderCapability::Simple(true)),
notebook_document_sync: Some(types::OneOf::Left(NotebookDocumentSyncOptions {
save: Some(false),
Expand Down Expand Up @@ -354,3 +363,56 @@ impl SupportedCodeAction {
.into_iter()
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum SupportedCommand {
Debug,
Format,
FixAll,
OrganizeImports,
}
Comment on lines +367 to +373
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed this from Command to SupportedCommand to match SupportedCodeAction and also moved it from the execute_command.rs module. Feel free to disagree on anything.


impl SupportedCommand {
const fn label(self) -> &'static str {
match self {
Self::FixAll => "Fix all auto-fixable problems",
Self::Format => "Format document",
Self::OrganizeImports => "Format imports",
Self::Debug => "Print debug information",
}
}

/// Returns the identifier of the command.
const fn identifier(self) -> &'static str {
match self {
SupportedCommand::Format => "ruff.applyFormat",
SupportedCommand::FixAll => "ruff.applyAutofix",
SupportedCommand::OrganizeImports => "ruff.applyOrganizeImports",
SupportedCommand::Debug => "ruff.printDebugInformation",
}
}

/// Returns all the commands that the server currently supports.
const fn all() -> [SupportedCommand; 4] {
[
SupportedCommand::Format,
SupportedCommand::FixAll,
SupportedCommand::OrganizeImports,
SupportedCommand::Debug,
]
}
}

impl FromStr for SupportedCommand {
type Err = anyhow::Error;

fn from_str(name: &str) -> anyhow::Result<Self, Self::Err> {
Ok(match name {
"ruff.applyAutofix" => Self::FixAll,
"ruff.applyFormat" => Self::Format,
"ruff.applyOrganizeImports" => Self::OrganizeImports,
"ruff.printDebugInformation" => Self::Debug,
_ => return Err(anyhow::anyhow!("Invalid command `{name}`")),
})
}
}
49 changes: 8 additions & 41 deletions crates/ruff_server/src/server/api/requests/execute_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ use std::str::FromStr;

use crate::edit::WorkspaceEditTracker;
use crate::server::api::LSPResult;
use crate::server::client;
use crate::server::schedule::Task;
use crate::server::{client, SupportedCommand};
use crate::session::Session;
use crate::DIAGNOSTIC_NAME;
use crate::{edit::DocumentVersion, server};
use lsp_server::ErrorCode;
use lsp_types::{self as types, request as req};
use serde::Deserialize;

#[derive(Debug, PartialEq)]
enum Command {
Debug,
Format,
FixAll,
OrganizeImports,
}

pub(crate) struct ExecuteCommand;

#[derive(Deserialize)]
Expand All @@ -38,10 +30,10 @@ impl super::SyncRequestHandler for ExecuteCommand {
requester: &mut client::Requester,
params: types::ExecuteCommandParams,
) -> server::Result<Option<serde_json::Value>> {
let command =
Command::from_str(&params.command).with_failure_code(ErrorCode::InvalidParams)?;
let command = SupportedCommand::from_str(&params.command)
.with_failure_code(ErrorCode::InvalidParams)?;

if command == Command::Debug {
if command == SupportedCommand::Debug {
let output = debug_information(session);
notifier
.notify::<types::notification::LogMessage>(types::LogMessageParams {
Expand Down Expand Up @@ -74,7 +66,7 @@ impl super::SyncRequestHandler for ExecuteCommand {
return Ok(None);
};
match command {
Command::FixAll => {
SupportedCommand::FixAll => {
let fixes = super::code_action_resolve::fix_all_edit(
snapshot.query(),
snapshot.encoding(),
Expand All @@ -84,13 +76,13 @@ impl super::SyncRequestHandler for ExecuteCommand {
.set_fixes_for_document(fixes, snapshot.query().version())
.with_failure_code(ErrorCode::InternalError)?;
}
Command::Format => {
SupportedCommand::Format => {
let fixes = super::format::format_full_document(&snapshot)?;
edit_tracker
.set_fixes_for_document(fixes, version)
.with_failure_code(ErrorCode::InternalError)?;
}
Command::OrganizeImports => {
SupportedCommand::OrganizeImports => {
let fixes = super::code_action_resolve::organize_imports_edit(
snapshot.query(),
snapshot.encoding(),
Expand All @@ -100,7 +92,7 @@ impl super::SyncRequestHandler for ExecuteCommand {
.set_fixes_for_document(fixes, snapshot.query().version())
.with_failure_code(ErrorCode::InternalError)?;
}
Command::Debug => {
SupportedCommand::Debug => {
unreachable!("The debug command should have already been handled")
}
}
Expand All @@ -119,31 +111,6 @@ impl super::SyncRequestHandler for ExecuteCommand {
}
}

impl Command {
fn label(&self) -> &str {
match self {
Self::FixAll => "Fix all auto-fixable problems",
Self::Format => "Format document",
Self::OrganizeImports => "Format imports",
Self::Debug => "Print debug information",
}
}
}

impl FromStr for Command {
type Err = anyhow::Error;

fn from_str(name: &str) -> Result<Self, Self::Err> {
Ok(match name {
"ruff.applyAutofix" => Self::FixAll,
"ruff.applyFormat" => Self::Format,
"ruff.applyOrganizeImports" => Self::OrganizeImports,
"ruff.printDebugInformation" => Self::Debug,
_ => return Err(anyhow::anyhow!("Invalid command `{name}`")),
})
}
}

fn apply_edit(
requester: &mut client::Requester,
label: &str,
Expand Down
Loading