switch redundant current! usage to doc! (#1416)

imgbot
Kirawi 3 years ago committed by GitHub
parent aaa42e1a69
commit 93a948d889
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1545,7 +1545,7 @@ fn searcher(cx: &mut Context, direction: Direction) {
let reg = cx.register.unwrap_or('/'); let reg = cx.register.unwrap_or('/');
let scrolloff = cx.editor.config.scrolloff; let scrolloff = cx.editor.config.scrolloff;
let (_, doc) = current!(cx.editor); let doc = doc!(cx.editor);
// TODO: could probably share with select_on_matches? // TODO: could probably share with select_on_matches?
@ -2046,7 +2046,7 @@ pub mod cmd {
fn write_impl(cx: &mut compositor::Context, path: Option<&Cow<str>>) -> anyhow::Result<()> { fn write_impl(cx: &mut compositor::Context, path: Option<&Cow<str>>) -> anyhow::Result<()> {
let jobs = &mut cx.jobs; let jobs = &mut cx.jobs;
let (_, doc) = current!(cx.editor); let doc = doc_mut!(cx.editor);
if let Some(ref path) = path { if let Some(ref path) = path {
doc.set_path(Some(path.as_ref().as_ref())) doc.set_path(Some(path.as_ref().as_ref()))
@ -2099,8 +2099,7 @@ pub mod cmd {
_args: &[Cow<str>], _args: &[Cow<str>],
_event: PromptEvent, _event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let (_, doc) = current!(cx.editor); let doc = doc!(cx.editor);
if let Some(format) = doc.format() { if let Some(format) = doc.format() {
let callback = let callback =
make_format_callback(doc.id(), doc.version(), Modified::LeaveModified, format); make_format_callback(doc.id(), doc.version(), Modified::LeaveModified, format);
@ -2409,7 +2408,7 @@ pub mod cmd {
args: &[Cow<str>], args: &[Cow<str>],
_event: PromptEvent, _event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let (_, doc) = current!(cx.editor); let doc = doc!(cx.editor);
let default_sep = Cow::Borrowed(doc.line_ending.as_str()); let default_sep = Cow::Borrowed(doc.line_ending.as_str());
let separator = args.first().unwrap_or(&default_sep); let separator = args.first().unwrap_or(&default_sep);
yank_joined_to_clipboard_impl(cx.editor, separator, ClipboardType::Clipboard) yank_joined_to_clipboard_impl(cx.editor, separator, ClipboardType::Clipboard)
@ -2428,7 +2427,7 @@ pub mod cmd {
args: &[Cow<str>], args: &[Cow<str>],
_event: PromptEvent, _event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let (_, doc) = current!(cx.editor); let doc = doc!(cx.editor);
let default_sep = Cow::Borrowed(doc.line_ending.as_str()); let default_sep = Cow::Borrowed(doc.line_ending.as_str());
let separator = args.first().unwrap_or(&default_sep); let separator = args.first().unwrap_or(&default_sep);
yank_joined_to_clipboard_impl(cx.editor, separator, ClipboardType::Selection) yank_joined_to_clipboard_impl(cx.editor, separator, ClipboardType::Selection)
@ -2555,7 +2554,7 @@ pub mod cmd {
args: &[Cow<str>], args: &[Cow<str>],
_event: PromptEvent, _event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let (_, doc) = current!(cx.editor); let doc = doc_mut!(cx.editor);
if let Some(label) = args.first() { if let Some(label) = args.first() {
doc.set_encoding(label) doc.set_encoding(label)
} else { } else {
@ -3247,7 +3246,7 @@ fn symbol_picker(cx: &mut Context) {
nested_to_flat(list, file, child); nested_to_flat(list, file, child);
} }
} }
let (_, doc) = current!(cx.editor); let doc = doc!(cx.editor);
let language_server = match doc.language_server() { let language_server = match doc.language_server() {
Some(language_server) => language_server, Some(language_server) => language_server,
@ -3268,7 +3267,7 @@ fn symbol_picker(cx: &mut Context) {
let symbols = match symbols { let symbols = match symbols {
lsp::DocumentSymbolResponse::Flat(symbols) => symbols, lsp::DocumentSymbolResponse::Flat(symbols) => symbols,
lsp::DocumentSymbolResponse::Nested(symbols) => { lsp::DocumentSymbolResponse::Nested(symbols) => {
let (_view, doc) = current!(editor); let doc = doc!(editor);
let mut flat_symbols = Vec::new(); let mut flat_symbols = Vec::new();
for symbol in symbols { for symbol in symbols {
nested_to_flat(&mut flat_symbols, &doc.identifier(), symbol) nested_to_flat(&mut flat_symbols, &doc.identifier(), symbol)
@ -3310,17 +3309,15 @@ fn symbol_picker(cx: &mut Context) {
} }
fn workspace_symbol_picker(cx: &mut Context) { fn workspace_symbol_picker(cx: &mut Context) {
let (_, doc) = current!(cx.editor); let doc = doc!(cx.editor);
let current_path = doc.path().cloned();
let language_server = match doc.language_server() { let language_server = match doc.language_server() {
Some(language_server) => language_server, Some(language_server) => language_server,
None => return, None => return,
}; };
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let future = language_server.workspace_symbols("".to_string()); let future = language_server.workspace_symbols("".to_string());
let current_path = doc_mut!(cx.editor).path().cloned();
cx.callback( cx.callback(
future, future,
move |_editor: &mut Editor, move |_editor: &mut Editor,
@ -3430,8 +3427,7 @@ pub fn code_action(cx: &mut Context) {
} }
pub fn execute_lsp_command(editor: &mut Editor, cmd: lsp::Command) { pub fn execute_lsp_command(editor: &mut Editor, cmd: lsp::Command) {
let (_view, doc) = current!(editor); let doc = doc!(editor);
let language_server = match doc.language_server() { let language_server = match doc.language_server() {
Some(language_server) => language_server, Some(language_server) => language_server,
None => return, None => return,
@ -4185,27 +4181,21 @@ fn goto_pos(editor: &mut Editor, pos: usize) {
} }
fn goto_first_diag(cx: &mut Context) { fn goto_first_diag(cx: &mut Context) {
let editor = &mut cx.editor; let doc = doc!(cx.editor);
let (_, doc) = current!(editor);
let pos = match doc.diagnostics().first() { let pos = match doc.diagnostics().first() {
Some(diag) => diag.range.start, Some(diag) => diag.range.start,
None => return, None => return,
}; };
goto_pos(cx.editor, pos);
goto_pos(editor, pos);
} }
fn goto_last_diag(cx: &mut Context) { fn goto_last_diag(cx: &mut Context) {
let editor = &mut cx.editor; let doc = doc!(cx.editor);
let (_, doc) = current!(editor);
let pos = match doc.diagnostics().last() { let pos = match doc.diagnostics().last() {
Some(diag) => diag.range.start, Some(diag) => diag.range.start,
None => return, None => return,
}; };
goto_pos(cx.editor, pos);
goto_pos(editor, pos);
} }
fn goto_next_diag(cx: &mut Context) { fn goto_next_diag(cx: &mut Context) {
@ -5270,7 +5260,7 @@ pub fn completion(cx: &mut Context) {
move |editor: &mut Editor, move |editor: &mut Editor,
compositor: &mut Compositor, compositor: &mut Compositor,
response: Option<lsp::CompletionResponse>| { response: Option<lsp::CompletionResponse>| {
let (_, doc) = current!(editor); let doc = doc!(editor);
if doc.mode() != Mode::Insert { if doc.mode() != Mode::Insert {
// we're not in insert mode anymore // we're not in insert mode anymore
return; return;

@ -781,8 +781,9 @@ impl EditorView {
pub fn clear_completion(&mut self, editor: &mut Editor) { pub fn clear_completion(&mut self, editor: &mut Editor) {
self.completion = None; self.completion = None;
// Clear any savepoints // Clear any savepoints
let (_, doc) = current!(editor); let doc = doc_mut!(editor);
doc.savepoint = None; doc.savepoint = None;
editor.clear_idle_timer(); // don't retrigger editor.clear_idle_timer(); // don't retrigger
} }
@ -940,14 +941,18 @@ impl EditorView {
} }
impl Component for EditorView { impl Component for EditorView {
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { fn handle_event(
let mut cxt = commands::Context { &mut self,
editor: cx.editor, event: Event,
context: &mut crate::compositor::Context,
) -> EventResult {
let mut cx = commands::Context {
editor: context.editor,
count: None, count: None,
register: None, register: None,
callback: None, callback: None,
on_next_key_callback: None, on_next_key_callback: None,
jobs: cx.jobs, jobs: context.jobs,
}; };
match event { match event {
@ -957,18 +962,19 @@ impl Component for EditorView {
EventResult::Consumed(None) EventResult::Consumed(None)
} }
Event::Key(key) => { Event::Key(key) => {
cxt.editor.reset_idle_timer(); cx.editor.reset_idle_timer();
let mut key = KeyEvent::from(key); let mut key = KeyEvent::from(key);
canonicalize_key(&mut key); canonicalize_key(&mut key);
// clear status // clear status
cxt.editor.status_msg = None; cx.editor.status_msg = None;
let (_, doc) = current!(cxt.editor); let doc = doc!(cx.editor);
let mode = doc.mode(); let mode = doc.mode();
if let Some(on_next_key) = self.on_next_key.take() { if let Some(on_next_key) = self.on_next_key.take() {
// if there's a command waiting input, do that first // if there's a command waiting input, do that first
on_next_key(&mut cxt, key); on_next_key(&mut cx, key);
} else { } else {
match mode { match mode {
Mode::Insert => { Mode::Insert => {
@ -980,8 +986,8 @@ impl Component for EditorView {
if let Some(completion) = &mut self.completion { if let Some(completion) = &mut self.completion {
// use a fake context here // use a fake context here
let mut cx = Context { let mut cx = Context {
editor: cxt.editor, editor: cx.editor,
jobs: cxt.jobs, jobs: cx.jobs,
scroll: None, scroll: None,
}; };
let res = completion.handle_event(event, &mut cx); let res = completion.handle_event(event, &mut cx);
@ -991,40 +997,40 @@ impl Component for EditorView {
if callback.is_some() { if callback.is_some() {
// assume close_fn // assume close_fn
self.clear_completion(cxt.editor); self.clear_completion(cx.editor);
} }
} }
} }
// if completion didn't take the event, we pass it onto commands // if completion didn't take the event, we pass it onto commands
if !consumed { if !consumed {
self.insert_mode(&mut cxt, key); self.insert_mode(&mut cx, key);
// lastly we recalculate completion // lastly we recalculate completion
if let Some(completion) = &mut self.completion { if let Some(completion) = &mut self.completion {
completion.update(&mut cxt); completion.update(&mut cx);
if completion.is_empty() { if completion.is_empty() {
self.clear_completion(cxt.editor); self.clear_completion(cx.editor);
} }
} }
} }
} }
mode => self.command_mode(mode, &mut cxt, key), mode => self.command_mode(mode, &mut cx, key),
} }
} }
self.on_next_key = cxt.on_next_key_callback.take(); self.on_next_key = cx.on_next_key_callback.take();
// appease borrowck // appease borrowck
let callback = cxt.callback.take(); let callback = cx.callback.take();
// if the command consumed the last view, skip the render. // if the command consumed the last view, skip the render.
// on the next loop cycle the Application will then terminate. // on the next loop cycle the Application will then terminate.
if cxt.editor.should_close() { if cx.editor.should_close() {
return EventResult::Ignored; return EventResult::Ignored;
} }
let (view, doc) = current!(cxt.editor); let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc, cxt.editor.config.scrolloff); view.ensure_cursor_in_view(doc, cx.editor.config.scrolloff);
// mode transitions // mode transitions
match (mode, doc.mode()) { match (mode, doc.mode()) {
@ -1053,7 +1059,7 @@ impl Component for EditorView {
EventResult::Consumed(callback) EventResult::Consumed(callback)
} }
Event::Mouse(event) => self.handle_mouse_event(event, &mut cxt), Event::Mouse(event) => self.handle_mouse_event(event, &mut cx),
} }
} }

Loading…
Cancel
Save