Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Fix #131 and #102 #132

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea
test/bin/
target/
doc/
Expand Down
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ features = ["dox", "embed-lgpl-docs"]
[dependencies]
libc = "0.2"
bitflags = "1.0"
once_cell = "1.0"

[dependencies.gtk-source-sys]
path = "./sourceview-sys"
Expand Down Expand Up @@ -91,3 +92,9 @@ git = "https://github.com/gtk-rs/gio"

[dev-dependencies]
gir-format-check = "^0.1"

[dev-dependencies.gtk]
git = "https://github.com/gtk-rs/gtk"

[dev-dependencies.glib]
git = "https://github.com/gtk-rs/glib"
30 changes: 30 additions & 0 deletions examples/autocomplete_words.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extern crate gio;
extern crate gtk;
extern crate sourceview;
use gio::prelude::*;
use gtk::prelude::*;
use sourceview::prelude::*;

use std::env;

fn main() {
let uiapp = gtk::Application::new(None, gio::ApplicationFlags::FLAGS_NONE)
.expect("Application::new failed");
uiapp.connect_activate(|app| {
let win = gtk::ApplicationWindow::new(app);
win.set_default_size(320, 200);
win.set_title("Basic example");

let sourceview = sourceview::View::new();
let completion = sourceview::CompletionWords::new(Some("Words"), None);
completion.register(&sourceview.get_buffer().expect("Sourceview has no buffer."));
sourceview
.get_completion()
.expect("Sourceview has no completion")
.add_provider(&completion);
win.add(&sourceview);

win.show_all();
});
uiapp.run(&env::args().collect::<Vec<_>>());
}
168 changes: 168 additions & 0 deletions examples/custom_autocompletion_subclass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
extern crate gio;
extern crate glib;
extern crate gtk;
extern crate sourceview;

use gio::prelude::*;
use glib::glib_object_wrapper;
use glib::subclass::types::ObjectSubclass;
use glib::translate::*;
use gtk::prelude::*;
use sourceview::prelude::*;
use sourceview::subclass::CompletionProviderImpl;

use glib::subclass::object::ObjectImpl;
use glib::GString;
use sourceview::{CompletionInfo, CompletionProvider};
use std::env;

fn main() {
let uiapp = gtk::Application::new(None, gio::ApplicationFlags::FLAGS_NONE)
.expect("Application::new failed");
uiapp.connect_activate(|app| {
let win = gtk::ApplicationWindow::new(app);
win.set_default_size(320, 200);
win.set_title("Basic example");

let sourceview = sourceview::View::new();
let completion = CustomAutocomplete::new();
let _ = sourceview
.get_completion()
.expect("Sourceview has no completion")
.add_provider(&completion);
win.add(&sourceview);

win.show_all();
});
uiapp.run(&env::args().collect::<Vec<_>>());
}

glib::glib_wrapper! {
pub struct CustomAutocomplete(Object<glib::subclass::simple::InstanceStruct<imp::CustomAutocomplete>, glib::subclass::simple::ClassStruct<imp::CustomAutocomplete>, CustomAutocompleteClass>) @implements crate::CompletionProvider;

match fn {
get_type => || imp::CustomAutocomplete::get_type().to_glib(),
}
}

impl CustomAutocomplete {
fn new() -> CustomAutocomplete {
glib::Object::new(Self::static_type(), &[])
.expect("Failed to create CustomAutocomplete instance.")
.downcast()
.expect("Created CustomAutocomplete is of wrong type.")
}
}

mod imp {
use super::*;
use sourceview::CompletionProposal;

pub struct CustomAutocomplete;

impl ObjectSubclass for CustomAutocomplete {
const NAME: &'static str = "CustomAutocomplete";
type ParentType = glib::Object;
type Instance = glib::subclass::simple::InstanceStruct<Self>;
type Class = glib::subclass::simple::ClassStruct<Self>;

glib::glib_object_subclass!();

fn type_init(type_: &mut glib::subclass::InitializingType<Self>) {
type_.add_interface::<CompletionProvider>();
}

fn new() -> Self {
CustomAutocomplete
}
}

impl ObjectImpl for CustomAutocomplete {
glib::glib_object_impl!();
}

impl CompletionProviderImpl for CustomAutocomplete {
fn get_name(&self, _obj: &CompletionProvider) -> GString {
GString::from("CustomAutocomplete")
}

fn get_icon(&self, _obj: &CompletionProvider) -> Option<gdk_pixbuf::Pixbuf> {
None
}

fn get_icon_name(&self, _obj: &CompletionProvider) -> Option<GString> {
None
}

fn get_gicon(&self, _obg: &CompletionProvider) -> Option<gio::Icon> {
None
}

fn populate(&self, obg: &CompletionProvider, context: &sourceview::CompletionContext) {
for i in 1..10 {
let item = sourceview::CompletionItem::new(
&format!("Test {}", i),
&format!("Test {};", i),
None,
Some("This is a test proposal"),
);
context.add_proposals(obg, &[item.upcast::<CompletionProposal>()], i == 9);
}
}

fn get_activation(&self, _obg: &CompletionProvider) -> sourceview::CompletionActivation {
sourceview::CompletionActivation::all()
}

fn provide_match(
&self,
_obg: &CompletionProvider,
_context: &sourceview::CompletionContext,
) -> bool {
true
}

fn get_info_widget(
&self,
_obg: &CompletionProvider,
_proposal: &sourceview::CompletionProposal,
) -> Option<gtk::Widget> {
None
}

fn update_info(
&self,
_obg: &CompletionProvider,
_proposal: &sourceview::CompletionProposal,
_info: &CompletionInfo,
) {
}

fn get_start_iter(
&self,
_obg: &CompletionProvider,
_context: &sourceview::CompletionContext,
_proposal: &sourceview::CompletionProposal,
_iter: &gtk::TextIter,
) -> bool {
false
}

fn activate_proposal(
&self,
_obg: &CompletionProvider,
_proposal: &sourceview::CompletionProposal,
_iter: &gtk::TextIter,
) -> bool {
false
}

fn get_interactive_delay(&self, _obj: &CompletionProvider) -> i32 {
0
}

fn get_priority(&self, _obg: &CompletionProvider) -> i32 {
1
}
}
}
23 changes: 23 additions & 0 deletions examples/plain_sourceview.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extern crate gio;
extern crate gtk;
extern crate sourceview;
use gio::prelude::*;
use gtk::prelude::*;

use std::env;

fn main() {
let uiapp = gtk::Application::new(None, gio::ApplicationFlags::FLAGS_NONE)
.expect("Application::new failed");
uiapp.connect_activate(|app| {
let win = gtk::ApplicationWindow::new(app);
win.set_default_size(320, 200);
win.set_title("Basic example");

let sourceview = sourceview::View::new();
win.add(&sourceview);

win.show_all();
});
uiapp.run(&env::args().collect::<Vec<_>>());
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern crate gobject_sys;
extern crate gtk;
extern crate gtk_source_sys;
extern crate gtk_sys;
extern crate once_cell;
extern crate pango;

extern crate libc;
Expand Down Expand Up @@ -56,6 +57,8 @@ pub mod prelude {
pub use file_saver::FileSaverExtManual;
}

pub mod subclass;

#[macro_use]
mod rt;

Expand Down
Loading