Skip to content

Commit

Permalink
Adjust syntect-plugin for trishume/syntect#182
Browse files Browse the repository at this point in the history
  • Loading branch information
robinst committed Jul 19, 2018
1 parent 508bbfc commit 771eefb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion rust/syntect-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ serde_derive = "1.0"
toml = "0.4"

[dependencies.syntect]
version = "2.0"
#version = "2.0"
default-features = false
features = ["parsing", "assets","dump-load-rs"]
path = "/Users/rstocker/Projects/rust/syntect-3"

[dependencies.xi-plugin-lib]
path = "../plugin-lib"
Expand Down
4 changes: 2 additions & 2 deletions rust/syntect-plugin/examples/make_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::path::{Path, PathBuf};

use xi_core::plugin_manifest::*;
use xi_core::LanguageDefinition;
use syntect::parsing::{SyntaxSet, SyntaxDefinition};
use syntect::parsing::{SyntaxSet, SyntaxReference};
use toml::Value;

const OUT_FILE_NAME: &str = "generated_manifest.toml";
Expand Down Expand Up @@ -68,7 +68,7 @@ fn main() -> Result<(), io::Error> {
f.write_all(toml_str.as_ref())
}

fn lang_from_syn<'a>(src: &'a SyntaxDefinition) -> LanguageDefinition {
fn lang_from_syn<'a>(src: &'a SyntaxReference) -> LanguageDefinition {
LanguageDefinition {
name: src.name.as_str().into(),
extensions: src.file_extensions.clone(),
Expand Down
28 changes: 14 additions & 14 deletions rust/syntect-plugin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ use xi_trace::{trace, trace_block};
use xi_plugin_lib::{Cache, Plugin, StateCache, View, mainloop};

use syntect::parsing::{ParseState, ScopeStack, SyntaxSet, SCOPE_REPO,
SyntaxDefinition, ScopeRepository};
SyntaxReference, ScopeRepository};
use stackmap::{StackMap, LookupResult};

const LINES_PER_RPC: usize = 10;
const INDENTATION_PRIORITY: u64 = 100;

/// The state for syntax highlighting of one file.
struct PluginState {
struct PluginState<'a> {
stack_idents: StackMap,
offset: usize,
initial_state: LineState,
initial_state: LineState<'a>,
spans_start: usize,
// unflushed spans
spans: Vec<ScopeSpan>,
Expand All @@ -60,15 +60,15 @@ type LockedRepo = MutexGuard<'static, ScopeRepository>;
// Note: this needs to be option because the caching layer relies on Default.
// We can't implement that because the actual initial state depends on the
// syntax. There are other ways to handle this, but this will do for now.
type LineState = Option<(ParseState, ScopeStack)>;
type LineState<'a> = Option<(ParseState<'a>, ScopeStack)>;

/// The state of syntax highlighting for a collection of buffers.
struct Syntect<'a> {
view_state: HashMap<ViewId, PluginState>,
view_state: HashMap<ViewId, PluginState<'a>>,
syntax_set: &'a SyntaxSet,
}

impl PluginState {
impl<'a> PluginState<'a> {
fn new() -> Self {
PluginState {
stack_idents: StackMap::default(),
Expand All @@ -81,7 +81,7 @@ impl PluginState {
}

// compute syntax for one line, also accumulating the style spans
fn compute_syntax(&mut self, line: &str, state: LineState) -> LineState {
fn compute_syntax(&mut self, line: &str, state: LineState<'a>) -> LineState<'a> {
let (mut parse_state, mut scope_state) = state.or_else(|| self.initial_state.clone()).unwrap();
let ops = parse_state.parse_line(&line);

Expand Down Expand Up @@ -127,7 +127,7 @@ impl PluginState {

#[allow(unused)]
// Return true if there's any more work to be done.
fn highlight_one_line(&mut self, ctx: &mut MyView) -> bool {
fn highlight_one_line(&mut self, ctx: &mut MyView<'a>) -> bool {
if let Some(line_num) = ctx.get_frontier() {
let (line_num, offset, state) = ctx.get_prev(line_num);
if offset != self.offset {
Expand Down Expand Up @@ -166,7 +166,7 @@ impl PluginState {
false
}

fn flush_spans(&mut self, ctx: &mut MyView) {
fn flush_spans(&mut self, ctx: &mut MyView<'a>) {
let _t = trace_block("PluginState::flush_spans", &["syntect"]);
if !self.new_scopes.is_empty() {
ctx.add_scopes(&self.new_scopes);
Expand All @@ -181,7 +181,7 @@ impl PluginState {
}
}

type MyView = View<StateCache<LineState>>;
type MyView<'a> = View<StateCache<LineState<'a>>>;

impl<'a> Syntect<'a> {
fn new(syntax_set: &'a SyntaxSet) -> Self {
Expand All @@ -192,10 +192,10 @@ impl<'a> Syntect<'a> {
}

/// Wipes any existing state and starts highlighting with `syntax`.
fn do_highlighting(&mut self, view: &mut MyView) {
fn do_highlighting(&mut self, view: &mut MyView<'a>) {
let initial_state = {
let syntax = self.guess_syntax(view.get_path());
Some((ParseState::new(syntax), ScopeStack::new()))
Some((ParseState::new(self.syntax_set, syntax), ScopeStack::new()))
};

let state = self.view_state.get_mut(&view.get_id()).unwrap();
Expand All @@ -208,7 +208,7 @@ impl<'a> Syntect<'a> {
view.schedule_idle();
}

fn guess_syntax(&'a self, path: Option<&Path>) -> &'a SyntaxDefinition {
fn guess_syntax(&self, path: Option<&Path>) -> &'a SyntaxReference {
let _t = trace_block("Syntect::guess_syntax", &["syntect"]);
match path {
Some(path) => self.syntax_set.find_syntax_for_file(path)
Expand Down Expand Up @@ -297,7 +297,7 @@ impl<'a> Syntect<'a> {


impl<'a> Plugin for Syntect<'a> {
type Cache = StateCache<LineState>;
type Cache = StateCache<LineState<'a>>;

fn new_view(&mut self, view: &mut View<Self::Cache>) {
let _t = trace_block("Syntect::new_view", &["syntect"]);
Expand Down

0 comments on commit 771eefb

Please sign in to comment.