fix(els): eliminate unwraps

* remove unused attr `metadata`
This commit is contained in:
Shunsuke Shibayama 2023-04-24 03:47:02 +09:00
parent 1d8ce2b38f
commit 321c83cb11

View file

@ -1,4 +1,4 @@
use std::fs::{metadata, File, Metadata}; use std::fs::File;
use std::io::Read; use std::io::Read;
use lsp_types::{ use lsp_types::{
@ -22,9 +22,7 @@ pub fn _get_code_from_uri(uri: &Url) -> ELSResult<String> {
let path = uri let path = uri
.to_file_path() .to_file_path()
.or_else(|_| util::denormalize(uri.clone()).to_file_path()) .or_else(|_| util::denormalize(uri.clone()).to_file_path())
.unwrap_or_else(|_| { .map_err(|_| format!("invalid file path: {uri}"))?;
panic!("invalid file path: {uri}");
});
let mut code = String::new(); let mut code = String::new();
File::open(path.as_path())?.read_to_string(&mut code)?; File::open(path.as_path())?.read_to_string(&mut code)?;
Ok(code) Ok(code)
@ -34,7 +32,6 @@ pub fn _get_code_from_uri(uri: &Url) -> ELSResult<String> {
pub struct FileCacheEntry { pub struct FileCacheEntry {
pub code: String, pub code: String,
pub ver: i32, pub ver: i32,
pub metadata: Metadata,
pub token_stream: Option<TokenStream>, pub token_stream: Option<TokenStream>,
} }
@ -163,11 +160,6 @@ impl FileCache {
return; return;
} }
} }
let metadata = metadata(
uri.to_file_path()
.unwrap_or_else(|_| util::denormalize(uri.clone().raw()).to_file_path().unwrap()),
)
.unwrap();
let token_stream = Lexer::from_str(code.clone()).lex().ok(); let token_stream = Lexer::from_str(code.clone()).lex().ok();
let ver = ver.unwrap_or({ let ver = ver.unwrap_or({
if let Some(entry) = entry { if let Some(entry) = entry {
@ -181,7 +173,6 @@ impl FileCache {
FileCacheEntry { FileCacheEntry {
code, code,
ver, ver,
metadata,
token_stream, token_stream,
}, },
); );
@ -191,7 +182,6 @@ impl FileCache {
let Some(entry) = unsafe { self.files.as_mut() }.get_mut(uri) else { let Some(entry) = unsafe { self.files.as_mut() }.get_mut(uri) else {
return; return;
}; };
let metadata = metadata(uri.to_file_path().unwrap()).unwrap();
let mut code = entry.code.clone(); let mut code = entry.code.clone();
let start = util::pos_to_byte_index(&code, old.start); let start = util::pos_to_byte_index(&code, old.start);
let end = util::pos_to_byte_index(&code, old.end); let end = util::pos_to_byte_index(&code, old.end);
@ -199,7 +189,6 @@ impl FileCache {
let token_stream = Lexer::from_str(code.clone()).lex().ok(); let token_stream = Lexer::from_str(code.clone()).lex().ok();
entry.code = code; entry.code = code;
// entry.ver += 1; // entry.ver += 1;
entry.metadata = metadata;
entry.token_stream = token_stream; entry.token_stream = token_stream;
} }
@ -212,7 +201,6 @@ impl FileCache {
// crate::_log!("212: double update detected {}, {}, code:\n{}", entry.ver, params.text_document.version, entry.code); // crate::_log!("212: double update detected {}, {}, code:\n{}", entry.ver, params.text_document.version, entry.code);
return; return;
} }
let metadata = metadata(uri.to_file_path().unwrap()).unwrap();
let mut code = entry.code.clone(); let mut code = entry.code.clone();
for change in params.content_changes { for change in params.content_changes {
let range = change.range.unwrap(); let range = change.range.unwrap();
@ -223,7 +211,6 @@ impl FileCache {
let token_stream = Lexer::from_str(code.clone()).lex().ok(); let token_stream = Lexer::from_str(code.clone()).lex().ok();
entry.code = code; entry.code = code;
entry.ver = params.text_document.version; entry.ver = params.text_document.version;
entry.metadata = metadata;
entry.token_stream = token_stream; entry.token_stream = token_stream;
} }