This commit is contained in:
Dongdong Zhou 2022-05-06 10:39:09 +01:00
parent 3d334e9e61
commit 3af0028aff
6 changed files with 1 additions and 386 deletions

View File

@ -34,7 +34,7 @@
use lapce_core::command::{
EditCommand, FocusCommand, MotionModeCommand, MultiSelectionCommand,
};
use lapce_core::mode::{Mode, MotionMode, VisualMode};
use lapce_core::mode::{Mode, MotionMode};
pub use lapce_core::syntax::Syntax;
use lsp_types::CompletionTextEdit;
use lsp_types::{
@ -1875,15 +1875,6 @@ pub struct TabRect {
pub text_layout: PietTextLayout,
}
#[derive(Clone)]
pub struct RegisterContent {
#[allow(dead_code)]
kind: VisualMode,
#[allow(dead_code)]
content: Vec<String>,
}
#[derive(Clone)]
pub struct HighlightTextLayout {
pub layout: PietTextLayout,

View File

@ -11,7 +11,6 @@
pub mod history;
pub mod hover;
pub mod keypress;
pub mod lsp;
pub mod menu;
pub mod outline;
pub mod palette;

View File

@ -1,144 +0,0 @@
use anyhow::{anyhow, Result};
use druid::{WidgetId, WindowId};
use jsonrpc_lite::Id;
use lapce_rpc::buffer::BufferId;
use parking_lot::Mutex;
use std::{collections::HashMap, io::BufRead, io::Write, process::Child, sync::Arc};
use lsp_types::*;
use serde_json::Value;
pub type Callback = Box<dyn Callable>;
const HEADER_CONTENT_LENGTH: &str = "content-length";
const HEADER_CONTENT_TYPE: &str = "content-type";
pub enum LspHeader {
ContentType,
ContentLength(usize),
}
pub struct LspCatalog {
clients: HashMap<String, Arc<LspClient>>,
}
impl LspCatalog {
pub fn new(_window_id: WindowId, _tab_id: WidgetId) -> LspCatalog {
LspCatalog {
clients: HashMap::new(),
}
}
pub fn stop(&mut self) {
for (_, client) in self.clients.iter() {
let _ = client.state.lock().process.kill();
}
self.clients.clear();
}
}
pub trait Callable: Send {
fn call(self: Box<Self>, client: &LspClient, result: Result<Value>);
}
impl<F: Send + FnOnce(&LspClient, Result<Value>)> Callable for F {
fn call(self: Box<F>, client: &LspClient, result: Result<Value>) {
(*self)(client, result)
}
}
pub struct LspState {
#[allow(dead_code)]
next_id: u64,
#[allow(dead_code)]
writer: Box<dyn Write + Send>,
process: Child,
#[allow(dead_code)]
pending: HashMap<u64, Callback>,
pub server_capabilities: Option<ServerCapabilities>,
pub opened_documents: HashMap<BufferId, Url>,
pub is_initialized: bool,
}
pub struct LspClient {
#[allow(dead_code)]
window_id: WindowId,
#[allow(dead_code)]
tab_id: WidgetId,
#[allow(dead_code)]
language_id: String,
#[allow(dead_code)]
options: Option<Value>,
state: Arc<Mutex<LspState>>,
}
#[allow(dead_code)]
fn prepare_lsp_json(msg: &Value) -> Result<String> {
let request = serde_json::to_string(&msg)?;
Ok(format!(
"Content-Length: {}\r\n\r\n{}",
request.len(),
request
))
}
fn parse_header(s: &str) -> Result<LspHeader> {
let split: Vec<String> =
s.splitn(2, ": ").map(|s| s.trim().to_lowercase()).collect();
if split.len() != 2 {
return Err(anyhow!("Malformed"));
};
match split[0].as_ref() {
HEADER_CONTENT_TYPE => Ok(LspHeader::ContentType),
HEADER_CONTENT_LENGTH => {
Ok(LspHeader::ContentLength(split[1].parse::<usize>()?))
}
_ => Err(anyhow!("Unknown parse error occurred")),
}
}
pub fn read_message<T: BufRead>(reader: &mut T) -> Result<String> {
let mut buffer = String::new();
let mut content_length: Option<usize> = None;
loop {
buffer.clear();
let _result = reader.read_line(&mut buffer);
// eprin
match &buffer {
s if s.trim().is_empty() => break,
s => {
match parse_header(s)? {
LspHeader::ContentLength(len) => content_length = Some(len),
LspHeader::ContentType => (),
};
}
};
}
let content_length = content_length
.ok_or_else(|| anyhow!("missing content-length header: {}", buffer))?;
let mut body_buffer = vec![0; content_length];
reader.read_exact(&mut body_buffer)?;
let body = String::from_utf8(body_buffer)?;
Ok(body)
}
#[allow(dead_code)]
fn number_from_id(id: &Id) -> u64 {
match *id {
Id::Num(n) => n as u64,
Id::Str(ref s) => s
.parse::<u64>()
.expect("failed to convert string id to u64"),
_ => panic!("unexpected value for id: None"),
}
}

View File

@ -759,77 +759,3 @@ fn file_get_head(workspace_path: &Path, path: &Path) -> Result<(String, String)>
.to_string();
Ok((id, content))
}
#[allow(dead_code)]
fn file_git_diff(
workspace_path: &Path,
path: &Path,
content: &str,
) -> Option<(Vec<DiffHunk>, HashMap<usize, char>)> {
let repo = Repository::open(workspace_path.to_str()?).ok()?;
let head = repo.head().ok()?;
let tree = head.peel_to_tree().ok()?;
let tree_entry = tree
.get_path(path.strip_prefix(workspace_path).ok()?)
.ok()?;
let blob = repo.find_blob(tree_entry.id()).ok()?;
let patch = git2::Patch::from_blob_and_buffer(
&blob,
None,
content.as_bytes(),
None,
None,
)
.ok()?;
let mut line_changes = HashMap::new();
Some((
(0..patch.num_hunks())
.into_iter()
.filter_map(|i| {
let hunk = patch.hunk(i).ok()?;
let hunk = DiffHunk {
old_start: hunk.0.old_start(),
old_lines: hunk.0.old_lines(),
new_start: hunk.0.new_start(),
new_lines: hunk.0.new_lines(),
header: String::from_utf8(hunk.0.header().to_vec()).ok()?,
};
let mut line_diff = 0;
for line in 0..hunk.old_lines + hunk.new_lines {
if let Ok(diff_line) = patch.line_in_hunk(i, line as usize) {
match diff_line.origin() {
' ' => {
let new_line = diff_line.new_lineno().unwrap();
let old_line = diff_line.old_lineno().unwrap();
line_diff = new_line as i32 - old_line as i32;
}
'-' => {
let old_line = diff_line.old_lineno().unwrap() - 1;
let new_line =
(old_line as i32 + line_diff) as usize;
line_changes.insert(new_line, '-');
line_diff -= 1;
}
'+' => {
let new_line =
diff_line.new_lineno().unwrap() as usize - 1;
if let Some(c) = line_changes.get(&new_line) {
if c == &'-' {
line_changes.insert(new_line, 'm');
}
} else {
line_changes.insert(new_line, '+');
}
line_diff += 1;
}
_ => continue,
}
diff_line.origin();
}
}
Some(hunk)
})
.collect(),
line_changes,
))
}

View File

@ -7,7 +7,6 @@
LifeCycle, LifeCycleCtx, PaintCtx, Point, Rect, RenderContext, Size, Target,
UpdateCtx, Widget, WidgetExt, WidgetId, WidgetPod,
};
use include_dir::{include_dir, Dir};
use lapce_data::{
command::LapceUICommand,
command::LAPCE_UI_COMMAND,
@ -24,9 +23,6 @@
svg::{file_svg_new, get_svg},
};
#[allow(dead_code)]
const ICONS_DIR: Dir = include_dir!("../icons");
#[allow(clippy::too_many_arguments)]
pub fn paint_file_node_item(
ctx: &mut PaintCtx,

View File

@ -1,153 +0,0 @@
use anyhow::{anyhow, Result};
use druid::{WidgetId, WindowId};
use jsonrpc_lite::Id;
use parking_lot::Mutex;
use std::{collections::HashMap, io::BufRead, io::Write, process::Child, sync::Arc};
use lsp_types::*;
use serde_json::Value;
use crate::buffer::BufferId;
pub type Callback = Box<dyn Callable>;
const HEADER_CONTENT_LENGTH: &str = "content-length";
const HEADER_CONTENT_TYPE: &str = "content-type";
pub enum LspHeader {
ContentType,
ContentLength(usize),
}
pub struct LspCatalog {
#[allow(dead_code)]
window_id: WindowId,
#[allow(dead_code)]
tab_id: WidgetId,
clients: HashMap<String, Arc<LspClient>>,
}
impl LspCatalog {
pub fn new(window_id: WindowId, tab_id: WidgetId) -> LspCatalog {
LspCatalog {
window_id,
tab_id,
clients: HashMap::new(),
}
}
pub fn stop(&mut self) {
for (_, client) in self.clients.iter() {
let _ = client.state.lock().process.kill();
}
self.clients.clear();
}
}
pub trait Callable: Send {
fn call(self: Box<Self>, client: &LspClient, result: Result<Value>);
}
impl<F: Send + FnOnce(&LspClient, Result<Value>)> Callable for F {
fn call(self: Box<F>, client: &LspClient, result: Result<Value>) {
(*self)(client, result)
}
}
pub struct LspState {
#[allow(dead_code)]
next_id: u64,
#[allow(dead_code)]
writer: Box<dyn Write + Send>,
process: Child,
#[allow(dead_code)]
pending: HashMap<u64, Callback>,
pub server_capabilities: Option<ServerCapabilities>,
pub opened_documents: HashMap<BufferId, Url>,
pub is_initialized: bool,
}
pub struct LspClient {
#[allow(dead_code)]
window_id: WindowId,
#[allow(dead_code)]
tab_id: WidgetId,
#[allow(dead_code)]
language_id: String,
#[allow(dead_code)]
options: Option<Value>,
state: Arc<Mutex<LspState>>,
}
#[allow(dead_code)]
fn prepare_lsp_json(msg: &Value) -> Result<String> {
let request = serde_json::to_string(&msg)?;
Ok(format!(
"Content-Length: {}\r\n\r\n{}",
request.len(),
request
))
}
fn parse_header(s: &str) -> Result<LspHeader> {
let split: Vec<String> =
s.splitn(2, ": ").map(|s| s.trim().to_lowercase()).collect();
if split.len() != 2 {
return Err(anyhow!("Malformed"));
};
match split[0].as_ref() {
HEADER_CONTENT_TYPE => Ok(LspHeader::ContentType),
HEADER_CONTENT_LENGTH => {
Ok(LspHeader::ContentLength(split[1].parse::<usize>()?))
}
_ => Err(anyhow!("Unknown parse error occurred")),
}
}
pub fn read_message<T: BufRead>(reader: &mut T) -> Result<String> {
let mut buffer = String::new();
let mut content_length: Option<usize> = None;
loop {
buffer.clear();
let _result = reader.read_line(&mut buffer);
// eprin
match &buffer {
s if s.trim().is_empty() => break,
s => {
match parse_header(s)? {
LspHeader::ContentLength(len) => content_length = Some(len),
LspHeader::ContentType => (),
};
}
};
}
let content_length = content_length
.ok_or_else(|| anyhow!("missing content-length header: {}", buffer))?;
let mut body_buffer = vec![0; content_length];
reader.read_exact(&mut body_buffer)?;
let body = String::from_utf8(body_buffer)?;
Ok(body)
}
#[allow(dead_code)]
fn number_from_id(id: &Id) -> u64 {
match *id {
Id::Num(n) => n as u64,
Id::Str(ref s) => s
.parse::<u64>()
.expect("failed to convert string id to u64"),
_ => panic!("unexpected value for id: None"),
}
}