Skip to content

Commit

Permalink
Move protocol types to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Dec 11, 2019
1 parent 7347d10 commit 5ac87b1
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 170 deletions.
61 changes: 4 additions & 57 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::capabilities::ClientCapabilitiesExt;
use crate::client::LspClient;
use crate::protocol_types::*;
use crate::workspace::*;
use futures::future::{AbortHandle, Abortable, Aborted};
use futures::lock::Mutex;
use futures::prelude::*;
use futures::stream;
use futures_boxed::boxed;
use lsp_types::*;
use serde::{Deserialize, Serialize};
use serde_repr::*;
use std::collections::HashMap;
use std::io;
use std::path::Path;
Expand All @@ -18,70 +17,18 @@ use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
use uuid::Uuid;

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildParams {
pub text_document: TextDocumentIdentifier,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildOptions {
pub executable: Option<String>,
pub args: Option<Vec<String>>,
pub on_save: Option<bool>,
}

impl BuildOptions {
pub fn executable(&self) -> String {
self.executable
.as_ref()
.map(Clone::clone)
.unwrap_or_else(|| "latexmk".to_owned())
}

pub fn args(&self) -> Vec<String> {
self.args.as_ref().map(Clone::clone).unwrap_or_else(|| {
vec![
"-pdf".to_owned(),
"-interaction=nonstopmode".to_owned(),
"-synctex=1".to_owned(),
]
})
}

pub fn on_save(&self) -> bool {
self.on_save.unwrap_or(false)
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize_repr, Deserialize_repr)]
#[repr(i32)]
pub enum BuildStatus {
Success = 0,
Error = 1,
Failure = 2,
Cancelled = 3,
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildResult {
pub status: BuildStatus,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BuildProvider<C> {
pub client: Arc<C>,
pub options: BuildOptions,
pub options: LatexBuildOptions,
pub token: ProgressToken,
}

impl<C> BuildProvider<C>
where
C: LspClient + Send + Sync + 'static,
{
pub fn new(client: Arc<C>, options: BuildOptions) -> Self {
pub fn new(client: Arc<C>, options: LatexBuildOptions) -> Self {
Self {
client,
options,
Expand Down Expand Up @@ -191,7 +138,7 @@ where
pub async fn build(
&self,
request: FeatureRequest<BuildParams>,
options: BuildOptions,
options: LatexBuildOptions,
) -> BuildResult {
let provider = BuildProvider::new(Arc::clone(&self.client), options);
let (handle, reg) = AbortHandle::new_pair();
Expand Down
10 changes: 1 addition & 9 deletions src/definition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use self::latex_command::LatexCommandDefinitionProvider;
use self::latex_label::LatexLabelDefinitionProvider;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::{Location, LocationLink, TextDocumentPositionParams};
use serde::{Deserialize, Serialize};
use lsp_types::{LocationLink, TextDocumentPositionParams};

pub struct DefinitionProvider {
provider: ConcatProvider<TextDocumentPositionParams, LocationLink>,
Expand Down Expand Up @@ -44,10 +43,3 @@ impl FeatureProvider for DefinitionProvider {
self.provider.execute(request).await
}
}

#[serde(untagged)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub enum DefinitionResponse {
Locations(Vec<Location>),
LocationLinks(Vec<LocationLink>),
}
18 changes: 0 additions & 18 deletions src/diagnostics/latex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,10 @@ use crate::workspace::{Document, Uri};
use lsp_types::*;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::{Read, Write};
use std::process::{Command, Stdio};

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct LatexLintOptions {
pub on_change: Option<bool>,
pub on_save: Option<bool>,
}

impl LatexLintOptions {
pub fn on_change(&self) -> bool {
self.on_change.unwrap_or(false)
}

pub fn on_save(&self) -> bool {
self.on_save.unwrap_or(false)
}
}

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct LatexDiagnosticsProvider {
diagnostics_by_uri: HashMap<Uri, Vec<Diagnostic>>,
Expand Down
4 changes: 2 additions & 2 deletions src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ mod bibtex;
mod build;
mod latex;

use self::bibtex::BibtexDiagnosticsProvider;
pub use self::bibtex::BibtexErrorCode;

use self::bibtex::BibtexDiagnosticsProvider;
use self::build::BuildDiagnosticsProvider;
use self::latex::LatexDiagnosticsProvider;
pub use self::latex::LatexLintOptions;
use crate::workspace::Document;
use lsp_types::Diagnostic;

Expand Down
11 changes: 2 additions & 9 deletions src/formatting/bibtex.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use crate::protocol_types::BibtexFormattingOptions;
use crate::syntax::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BibtexFormattingOptions {
pub line_length: Option<i32>,
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BibtexFormattingParams {
pub tab_size: usize,
pub insert_spaces: bool,
Expand Down
25 changes: 2 additions & 23 deletions src/forward_search.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
use crate::protocol_types::*;
use log::*;
use serde::{Deserialize, Serialize};
use serde_repr::*;
use std::io;
use std::path::Path;
use std::process::Stdio;
use tokio::process::Command;

#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
pub struct ForwardSearchOptions {
pub executable: Option<String>,
pub args: Option<Vec<String>>,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize_repr, Deserialize_repr)]
#[repr(i32)]
pub enum ForwardSearchStatus {
Success = 0,
Error = 1,
Failure = 2,
Unconfigured = 3,
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct ForwardSearchResult {
pub status: ForwardSearchStatus,
}

pub async fn search<'a>(
tex_file: &'a Path,
parent: &'a Path,
line_number: u64,
options: ForwardSearchOptions,
options: LatexForwardSearchOptions,
) -> Option<ForwardSearchResult> {
if options.executable.is_none() || options.args.is_none() {
return Some(ForwardSearchResult {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod highlight;
pub mod hover;
pub mod link;
pub mod lsp_kind;
pub mod protocol_types;
pub mod range;
pub mod reference;
pub mod rename;
Expand Down
105 changes: 105 additions & 0 deletions src/protocol_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use lsp_types::{Location, LocationLink, TextDocumentIdentifier};
use serde::{Deserialize, Serialize};
use serde_repr::*;

#[serde(untagged)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub enum DefinitionResponse {
Locations(Vec<Location>),
LocationLinks(Vec<LocationLink>),
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BibtexFormattingOptions {
pub line_length: Option<i32>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
pub struct LatexForwardSearchOptions {
pub executable: Option<String>,
pub args: Option<Vec<String>>,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize_repr, Deserialize_repr)]
#[repr(i32)]
pub enum ForwardSearchStatus {
Success = 0,
Error = 1,
Failure = 2,
Unconfigured = 3,
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct ForwardSearchResult {
pub status: ForwardSearchStatus,
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct LatexLintOptions {
pub on_change: Option<bool>,
pub on_save: Option<bool>,
}

impl LatexLintOptions {
pub fn on_change(&self) -> bool {
self.on_change.unwrap_or(false)
}

pub fn on_save(&self) -> bool {
self.on_save.unwrap_or(false)
}
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildParams {
pub text_document: TextDocumentIdentifier,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LatexBuildOptions {
pub executable: Option<String>,
pub args: Option<Vec<String>>,
pub on_save: Option<bool>,
}

impl LatexBuildOptions {
pub fn executable(&self) -> String {
self.executable
.as_ref()
.map(Clone::clone)
.unwrap_or_else(|| "latexmk".to_owned())
}

pub fn args(&self) -> Vec<String> {
self.args.as_ref().map(Clone::clone).unwrap_or_else(|| {
vec![
"-pdf".to_owned(),
"-interaction=nonstopmode".to_owned(),
"-synctex=1".to_owned(),
]
})
}

pub fn on_save(&self) -> bool {
self.on_save.unwrap_or(false)
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize_repr, Deserialize_repr)]
#[repr(i32)]
pub enum BuildStatus {
Success = 0,
Error = 1,
Failure = 2,
Cancelled = 3,
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildResult {
pub status: BuildStatus,
}
24 changes: 14 additions & 10 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ use crate::capabilities::ClientCapabilitiesExt;
use crate::citeproc::render_citation;
use crate::client::LspClient;
use crate::completion::{CompletionItemData, CompletionProvider, DATABASE};
use crate::definition::{DefinitionProvider, DefinitionResponse};
use crate::diagnostics::{DiagnosticsManager, LatexLintOptions};
use crate::definition::DefinitionProvider;
use crate::diagnostics::DiagnosticsManager;
use crate::folding::FoldingProvider;
use crate::formatting::bibtex::{self, BibtexFormattingOptions, BibtexFormattingParams};
use crate::forward_search::{self, ForwardSearchOptions, ForwardSearchResult, ForwardSearchStatus};
use crate::formatting::bibtex::{self, BibtexFormattingParams};
use crate::forward_search;
use crate::highlight::HighlightProvider;
use crate::hover::HoverProvider;
use crate::link::LinkProvider;
use crate::protocol_types::*;
use crate::reference::ReferenceProvider;
use crate::rename::{PrepareRenameProvider, RenameProvider};
use crate::symbol::{self, SymbolProvider, SymbolResponse};
use crate::symbol::{self, SymbolProvider};
use crate::syntax::*;
use crate::workspace::*;
use futures::lock::Mutex;
Expand Down Expand Up @@ -309,10 +310,13 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
}

#[jsonrpc_method("textDocument/documentSymbol", kind = "request")]
pub async fn document_symbol(&self, params: DocumentSymbolParams) -> Result<SymbolResponse> {
pub async fn document_symbol(
&self,
params: DocumentSymbolParams,
) -> Result<DocumentSymbolResponse> {
let request = self.make_feature_request(params.text_document.as_uri(), params)?;
let symbols = self.symbol_provider.execute(&request).await;
let response = SymbolResponse::new(
let response = symbol::document_symbols(
&self.client_capabilities.get().unwrap(),
&request.view.workspace,
&request.document().uri,
Expand Down Expand Up @@ -383,7 +387,7 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
#[jsonrpc_method("textDocument/build", kind = "request")]
pub async fn build(&self, params: BuildParams) -> Result<BuildResult> {
let request = self.make_feature_request(params.text_document.as_uri(), params)?;
let options = self.configuration::<BuildOptions>("latex.build").await;
let options = self.configuration::<LatexBuildOptions>("latex.build").await;
let result = self.build_manager.build(request, options).await;
Ok(result)
}
Expand All @@ -395,7 +399,7 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
) -> Result<ForwardSearchResult> {
let request = self.make_feature_request(params.text_document.as_uri(), params)?;
let options = self
.configuration::<ForwardSearchOptions>("latex.forwardSearch")
.configuration::<LatexForwardSearchOptions>("latex.forwardSearch")
.await;

match request.document().uri.to_file_path() {
Expand Down Expand Up @@ -623,7 +627,7 @@ impl<C: LspClient + Send + Sync + 'static> Middleware for LatexLspServer<C> {
}
}
Action::Build(uri) => {
let config: BuildOptions = self.configuration("latex.build").await;
let config: LatexBuildOptions = self.configuration("latex.build").await;
if config.on_save() {
let text_document = TextDocumentIdentifier::new(uri.into());
self.build(BuildParams { text_document }).await.unwrap();
Expand Down
Loading

0 comments on commit 5ac87b1

Please sign in to comment.