Skip to content

Commit

Permalink
Added Initialize method to Plugin Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
betterclever authored and cmyr committed Jul 17, 2018
1 parent 0bc59f6 commit 508bbfc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
31 changes: 31 additions & 0 deletions rust/plugin-lib/src/core_proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A proxy for the methods on Core
use xi_rpc::{RpcCtx, RpcPeer};

#[derive(Clone)]
pub struct CoreProxy {
peer: RpcPeer
}

impl CoreProxy {

pub fn new(rpc_ctx: &RpcCtx) -> Self {
CoreProxy {
peer: rpc_ctx.get_peer().clone()
}
}
}
5 changes: 5 additions & 0 deletions rust/plugin-lib/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use xi_core::{ViewId, PluginPid, ConfigTable};
use xi_core::plugin_rpc::{PluginBufferInfo, PluginUpdate, HostRequest, HostNotification};
use xi_rpc::{RpcCtx, RemoteError, Handler as RpcHandler};
use xi_trace::{self, trace, trace_block, trace_block_payload};
use core_proxy::CoreProxy;

use super::{Plugin, View};

Expand Down Expand Up @@ -72,6 +73,10 @@ impl<'a, P: 'a + Plugin> Dispatcher<'a, P> {
assert!(self.pid.is_none(), "initialize rpc received with existing pid");
eprintln!("Initializing plugin {:?}", plugin_id);
self.pid = Some(plugin_id);

let core_proxy = CoreProxy::new(ctx);
self.plugin.initialize(core_proxy);

self.do_new_buffer(ctx, buffers);
}

Expand Down
8 changes: 8 additions & 0 deletions rust/plugin-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod state_cache;
mod base_cache;
mod view;
mod dispatch;
mod core_proxy;

use std::io;
use std::path::Path;
Expand All @@ -44,6 +45,7 @@ use self::dispatch::Dispatcher;
pub use view::View;
pub use state_cache::StateCache;
pub use base_cache::ChunkCache;
pub use core_proxy::CoreProxy;

/// Abstracts getting data from the peer. Mainly exists for mocking in tests.
pub trait DataSource {
Expand Down Expand Up @@ -109,6 +111,12 @@ pub trait Cache {
pub trait Plugin {
type Cache: Cache;

/// Called when the Plugin is initialized. The plugin receives CoreProxy
/// object that is a wrapper around the RPC Peer and can be used to call
/// related methods on the Core in a type-safe manner.
#[allow(unused_variables)]
fn initialize(&mut self, core: CoreProxy) {}

/// Called when an edit has occurred in the remote view. If the plugin wishes
/// to add its own edit, it must do so using asynchronously via the edit notification.
fn update(&mut self, view: &mut View<Self::Cache>, delta: Option<&RopeDelta>,
Expand Down

0 comments on commit 508bbfc

Please sign in to comment.