Skip to content

Commit

Permalink
Rework code action support checker
Browse files Browse the repository at this point in the history
  • Loading branch information
snowsignal committed Apr 16, 2024
1 parent 3ef2889 commit c3d69f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 41 deletions.
37 changes: 13 additions & 24 deletions crates/ruff_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl Server {
CodeActionOptions {
code_action_kinds: Some(
SupportedCodeAction::all()
.flat_map(|action| action.kinds().iter())
.map(SupportedCodeAction::to_kind)
.cloned()
.collect(),
),
Expand Down Expand Up @@ -285,22 +285,24 @@ pub(crate) enum SupportedCodeAction {
}

impl SupportedCodeAction {
/// Returns the possible LSP code action kind(s) that map to this code action.
fn kinds(self) -> &'static [CodeActionKind] {
static QUICKFIX: [CodeActionKind; 1] = [CodeActionKind::QUICKFIX];
static SOURCE_FIX_ALL: [CodeActionKind; 2] =
[CodeActionKind::SOURCE_FIX_ALL, crate::SOURCE_FIX_ALL_RUFF];
static SOURCE_ORGANIZE_IMPORTS: [CodeActionKind; 2] = [
CodeActionKind::SOURCE_ORGANIZE_IMPORTS,
crate::SOURCE_ORGANIZE_IMPORTS_RUFF,
];
/// Returns the LSP code action kind that map to this code action.
fn to_kind(self) -> &'static CodeActionKind {
static QUICK_FIX: CodeActionKind = CodeActionKind::QUICKFIX;
static SOURCE_FIX_ALL: CodeActionKind = crate::SOURCE_FIX_ALL_RUFF;
static SOURCE_ORGANIZE_IMPORTS: CodeActionKind = crate::SOURCE_ORGANIZE_IMPORTS_RUFF;
match self {
Self::QuickFix => &QUICKFIX,
Self::QuickFix => &QUICK_FIX,
Self::SourceFixAll => &SOURCE_FIX_ALL,
Self::SourceOrganizeImports => &SOURCE_ORGANIZE_IMPORTS,
}
}

fn from_kind(kind: CodeActionKind) -> impl Iterator<Item = Self> {
Self::all().filter(move |supported_kind| {
supported_kind.to_kind().as_str().starts_with(kind.as_str())
})
}

/// Returns all code actions kinds that the server currently supports.
fn all() -> impl Iterator<Item = Self> {
[
Expand All @@ -311,16 +313,3 @@ impl SupportedCodeAction {
.into_iter()
}
}

impl TryFrom<CodeActionKind> for SupportedCodeAction {
type Error = ();

fn try_from(kind: CodeActionKind) -> std::result::Result<Self, Self::Error> {
for supported_kind in Self::all() {
if supported_kind.kinds().contains(&kind) {
return Ok(supported_kind);
}
}
Err(())
}
}
12 changes: 3 additions & 9 deletions crates/ruff_server/src/server/api/requests/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,8 @@ fn supported_code_actions(
return SupportedCodeAction::all().collect();
};

SupportedCodeAction::all()
.filter(move |action| {
action_filter.iter().any(|filter| {
action
.kinds()
.iter()
.any(|kind| kind.as_str().starts_with(filter.as_str()))
})
})
action_filter
.into_iter()
.flat_map(SupportedCodeAction::from_kind)
.collect()
}
18 changes: 10 additions & 8 deletions crates/ruff_server/src/server/api/requests/code_action_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ impl super::BackgroundDocumentRequestHandler for CodeActionResolve {
) -> Result<types::CodeAction> {
let document = snapshot.document();

let action_kind: SupportedCodeAction = action
.kind
.clone()
.ok_or(anyhow::anyhow!("No kind was given for code action"))
.with_failure_code(ErrorCode::InvalidParams)?
.try_into()
.map_err(|()| anyhow::anyhow!("Code action was of an invalid kind"))
.with_failure_code(ErrorCode::InvalidParams)?;
let action_kind = SupportedCodeAction::from_kind(
action
.kind
.clone()
.ok_or(anyhow::anyhow!("No kind was given for code action"))
.with_failure_code(ErrorCode::InvalidParams)?,
)
.next()
.ok_or(anyhow::anyhow!("Code action was of an invalid kind"))
.with_failure_code(ErrorCode::InvalidParams)?;

action.edit = match action_kind {
SupportedCodeAction::SourceFixAll => Some(
Expand Down

0 comments on commit c3d69f7

Please sign in to comment.