diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2021-12-19 02:44:42 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-18 16:14:42 -0500 |
commit | 6de53b631fcdb96d72639b6d2db3592d5fa8498d (patch) | |
tree | 9a93d868f5f434a4898f212cb6bd53e65ca49ce0 /cli/lsp/refactor.rs | |
parent | 3db18bf9e6466c74efd9052df4d372ea0b581154 (diff) |
refactor: use `once_cell` instead of `lazy_static` (#13135)
Diffstat (limited to 'cli/lsp/refactor.rs')
-rw-r--r-- | cli/lsp/refactor.rs | 167 |
1 files changed, 115 insertions, 52 deletions
diff --git a/cli/lsp/refactor.rs b/cli/lsp/refactor.rs index 17ab4d5de..2694b7c5a 100644 --- a/cli/lsp/refactor.rs +++ b/cli/lsp/refactor.rs @@ -7,6 +7,7 @@ use deno_core::serde::Deserialize; use deno_core::serde::Serialize; use deno_core::ModuleSpecifier; use lspower::lsp; +use once_cell::sync::Lazy; pub struct RefactorCodeActionKind { pub kind: lsp::CodeActionKind, @@ -19,58 +20,120 @@ impl RefactorCodeActionKind { } } -lazy_static::lazy_static! { - pub static ref EXTRACT_FUNCTION: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR_EXTRACT.as_str(), "function"].join(".").into(), +pub static EXTRACT_FUNCTION: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [lsp::CodeActionKind::REFACTOR_EXTRACT.as_str(), "function"] + .join(".") + .into(), matches_callback: Box::new(|tag: &str| tag.starts_with("function_")), - }; + }); - pub static ref EXTRACT_CONSTANT: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR_EXTRACT.as_str(), "constant"].join(".").into(), +pub static EXTRACT_CONSTANT: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [lsp::CodeActionKind::REFACTOR_EXTRACT.as_str(), "constant"] + .join(".") + .into(), matches_callback: Box::new(|tag: &str| tag.starts_with("constant_")), - }; - - pub static ref EXTRACT_TYPE: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR_EXTRACT.as_str(), "type"].join(".").into(), - matches_callback: Box::new(|tag: &str| tag.starts_with("Extract to type alias")), - }; - - pub static ref EXTRACT_INTERFACE: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR_EXTRACT.as_str(), "interface"].join(".").into(), - matches_callback: Box::new(|tag: &str| tag.starts_with("Extract to interface")), - }; - - pub static ref MOVE_NEWFILE: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR.as_str(), "move", "newFile"].join(".").into(), - matches_callback: Box::new(|tag: &str| tag.starts_with("Move to a new file")), - }; - - pub static ref REWRITE_IMPORT: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), "import"].join(".").into(), - matches_callback: Box::new(|tag: &str| tag.starts_with("Convert namespace import") || tag.starts_with("Convert named imports")), - }; - - pub static ref REWRITE_EXPORT: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), "export"].join(".").into(), - matches_callback: Box::new(|tag: &str| tag.starts_with("Convert default export") || tag.starts_with("Convert named export")), - }; - - pub static ref REWRITE_ARROW_BRACES: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), "arrow", "braces"].join(".").into(), - matches_callback: Box::new(|tag: &str| tag.starts_with("Add or remove braces in an arrow function")), - }; - - pub static ref REWRITE_PARAMETERS_TO_DESTRUCTURED: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), "parameters", "toDestructured"].join(".").into(), - matches_callback: Box::new(|tag: &str| tag.starts_with("Convert parameters to destructured object")), - }; - - pub static ref REWRITE_PROPERTY_GENERATEACCESSORS: RefactorCodeActionKind = RefactorCodeActionKind { - kind : [lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), "property", "generateAccessors"].join(".").into(), - matches_callback: Box::new(|tag: &str| tag.starts_with("Generate 'get' and 'set' accessors")), - }; - - pub static ref ALL_KNOWN_REFACTOR_ACTION_KINDS: Vec<&'static RefactorCodeActionKind> = vec![ + }); + +pub static EXTRACT_TYPE: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [lsp::CodeActionKind::REFACTOR_EXTRACT.as_str(), "type"] + .join(".") + .into(), + matches_callback: Box::new(|tag: &str| { + tag.starts_with("Extract to type alias") + }), + }); + +pub static EXTRACT_INTERFACE: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [lsp::CodeActionKind::REFACTOR_EXTRACT.as_str(), "interface"] + .join(".") + .into(), + matches_callback: Box::new(|tag: &str| { + tag.starts_with("Extract to interface") + }), + }); + +pub static MOVE_NEWFILE: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [lsp::CodeActionKind::REFACTOR.as_str(), "move", "newFile"] + .join(".") + .into(), + matches_callback: Box::new(|tag: &str| { + tag.starts_with("Move to a new file") + }), + }); + +pub static REWRITE_IMPORT: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), "import"] + .join(".") + .into(), + matches_callback: Box::new(|tag: &str| { + tag.starts_with("Convert namespace import") + || tag.starts_with("Convert named imports") + }), + }); + +pub static REWRITE_EXPORT: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), "export"] + .join(".") + .into(), + matches_callback: Box::new(|tag: &str| { + tag.starts_with("Convert default export") + || tag.starts_with("Convert named export") + }), + }); + +pub static REWRITE_ARROW_BRACES: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [ + lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), + "arrow", + "braces", + ] + .join(".") + .into(), + matches_callback: Box::new(|tag: &str| { + tag.starts_with("Add or remove braces in an arrow function") + }), + }); + +pub static REWRITE_PARAMETERS_TO_DESTRUCTURED: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [ + lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), + "parameters", + "toDestructured", + ] + .join(".") + .into(), + matches_callback: Box::new(|tag: &str| { + tag.starts_with("Convert parameters to destructured object") + }), + }); + +pub static REWRITE_PROPERTY_GENERATEACCESSORS: Lazy<RefactorCodeActionKind> = + Lazy::new(|| RefactorCodeActionKind { + kind: [ + lsp::CodeActionKind::REFACTOR_REWRITE.as_str(), + "property", + "generateAccessors", + ] + .join(".") + .into(), + matches_callback: Box::new(|tag: &str| { + tag.starts_with("Generate 'get' and 'set' accessors") + }), + }); + +pub static ALL_KNOWN_REFACTOR_ACTION_KINDS: Lazy< + Vec<&'static RefactorCodeActionKind>, +> = Lazy::new(|| { + vec![ &EXTRACT_FUNCTION, &EXTRACT_CONSTANT, &EXTRACT_TYPE, @@ -80,9 +143,9 @@ lazy_static::lazy_static! { &REWRITE_EXPORT, &REWRITE_ARROW_BRACES, &REWRITE_PARAMETERS_TO_DESTRUCTURED, - &REWRITE_PROPERTY_GENERATEACCESSORS - ]; -} + &REWRITE_PROPERTY_GENERATEACCESSORS, + ] +}); #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] |