Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
remove MoveFile (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirovmm authored Sep 6, 2021
1 parent db1ad87 commit 9b4697a
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 286 deletions.
148 changes: 74 additions & 74 deletions Cargo.lock

Large diffs are not rendered by default.

112 changes: 7 additions & 105 deletions lang/src/compiler/file.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use std::borrow::Cow;
use std::convert::TryFrom;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

use anyhow::{Error, Result};
use anyhow::Result;
use walkdir::{DirEntry, IntoIter};

pub struct Files<'a, P: AsRef<Path>> {
Expand Down Expand Up @@ -50,16 +45,6 @@ impl<'a, P: AsRef<Path>> Iterator for Files<'a, P> {
}
}

pub fn find_move_files<P>(paths: &[P]) -> Files<P>
where
P: AsRef<Path>,
{
Files {
paths,
walker_iter: None,
}
}

fn is_move_file(entry: &DirEntry) -> bool {
entry.file_type().is_file()
&& !entry
Expand All @@ -74,96 +59,17 @@ fn is_move_file(entry: &DirEntry) -> bool {
.unwrap_or(false)
}

#[derive(Debug, Clone, PartialEq)]
pub struct MoveFile<'n, 'c> {
name: Cow<'n, str>,
content: Cow<'c, str>,
}

impl<'n, 'c> MoveFile<'n, 'c> {
pub fn load<P: AsRef<Path>>(path: P) -> Result<MoveFile<'static, 'static>> {
let path = path.as_ref();
let mut f = File::open(&path)
.map_err(|err| std::io::Error::new(err.kind(), format!("{}: {:?}", err, path)))?;
let mut content = String::new();
f.read_to_string(&mut content)?;

let name = path
.to_str()
.map(|path| path.to_owned())
.ok_or_else(|| anyhow!("Failed to convert source path"))?;

Ok(MoveFile {
name: Cow::Owned(name),
content: Cow::Owned(content),
})
}

pub fn with_content<N, C>(name: N, content: C) -> MoveFile<'n, 'c>
where
N: Into<Cow<'n, str>>,
C: Into<Cow<'c, str>>,
{
MoveFile {
name: name.into(),
content: content.into(),
}
}

pub fn name(&self) -> &str {
&self.name
}

pub fn content(&self) -> &str {
&self.content
}

pub fn into(self) -> (String, String) {
(self.name.into_owned(), self.content.into_owned())
}
}

impl TryFrom<&PathBuf> for MoveFile<'static, 'static> {
type Error = Error;

fn try_from(value: &PathBuf) -> Result<Self, Self::Error> {
MoveFile::load(value)
}
}
// TODO refactoring. Make iterator like api to work with move files.

pub fn load_move_files_with_filter<P, F>(
paths: &[P],
filter: &F,
) -> Result<Vec<MoveFile<'static, 'static>>>
pub fn find_move_files<P>(paths: &[P]) -> Files<P>
where
F: Fn(&Path) -> bool,
P: AsRef<Path>,
{
let mut module_files = vec![];
for path in paths {
let path = path.as_ref();
anyhow::ensure!(
path.exists(),
"Cannot open {:?}: No such file or directory",
path
);
if path.is_file() {
module_files.push(MoveFile::load(path)?);
} else {
for path in find_move_files_with_filter(path, filter)? {
module_files.push(MoveFile::load(path)?);
}
}
Files {
paths,
walker_iter: None,
}
Ok(module_files)
}

pub fn load_move_files<P: AsRef<Path>>(paths: &[P]) -> Result<Vec<MoveFile<'static, 'static>>> {
load_move_files_with_filter(paths, &|_| true)
}

pub fn find_move_files_with_filter<P, F>(path: P, filter: &F) -> Result<Vec<String>>
pub fn find_move_files_with_filter<P, F>(path: P, filter: &F) -> Result<Vec<PathBuf>>
where
P: AsRef<Path>,
F: Fn(&Path) -> bool,
Expand All @@ -175,11 +81,7 @@ where
if is_move_file(&entry) {
let path = entry.into_path();
if filter(&path) {
Some(
path.into_os_string()
.into_string()
.map_err(|path| anyhow!("Failed to convert path:{:?}", path)),
)
Some(Ok(path))
} else {
None
}
Expand Down
16 changes: 7 additions & 9 deletions language_server/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ impl GlobalState {
let mut change = AnalysisChange::new();
for fs_event in fs_events {
match fs_event {
FileSystemEvent::AddFile(file) => {
let (fpath, text) = file.into();
change.add_file(fpath, text);
FileSystemEvent::AddFile(fpath) => {
change.add_file(fpath.to_string_lossy().to_string());
}
FileSystemEvent::ChangeFile(file) => {
let (fpath, text) = file.into();
change.update_file(fpath, text);
FileSystemEvent::ChangeFile(fpath) => {
change.update_file(fpath.to_string_lossy().to_string());
}
FileSystemEvent::RemoveFile(fpath) => {
change.remove_file(fpath);
change.remove_file(fpath.to_string_lossy().to_string());
}
}
}
Expand All @@ -69,13 +67,13 @@ pub fn initialize_new_global_state(config: Config) -> GlobalState {
let mut initial_fs_events = vec![];
match &config.stdlib_folder {
Some(folder) => {
for file in file::load_move_files(&[folder]).unwrap() {
for file in file::find_move_files(&[folder]) {
initial_fs_events.push(FileSystemEvent::AddFile(file));
}
}
None => {}
}
for file in file::load_move_files(&config.modules_folders).unwrap() {
for file in file::find_move_files(&config.modules_folders) {
initial_fs_events.push(FileSystemEvent::AddFile(file));
}
GlobalState::new(config, initial_fs_events)
Expand Down
19 changes: 9 additions & 10 deletions language_server/src/inner/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use move_lang::shared::Flags;

use lang::compiler::{check, file};
use lang::compiler::file::MoveFile;

use std::path::PathBuf;
use crate::inner::db::{FileDiagnostic, RootDatabase};

#[derive(Debug)]
Expand All @@ -19,24 +18,24 @@ impl Analysis {
&self.db
}

pub fn check_file(&self, file: MoveFile) -> Option<FileDiagnostic> {
pub fn check_file(&self, file: String) -> Option<FileDiagnostic> {
match self.check_file_inner(file) {
Ok(_) => None,
Err(mut ds) => Some(ds.remove(0)),
}
}

fn check_file_inner(&self, current_file: MoveFile) -> Result<(), Vec<FileDiagnostic>> {
fn check_file_inner(&self, current_file: String) -> Result<(), Vec<FileDiagnostic>> {
let deps = self
.read_stdlib_files()
.into_iter()
.map(|mf| mf.into().0)
.chain(self.db.module_files().into_iter().map(|(name, _)| name))
.filter(|file| file != current_file.name())
.map(|path| path.to_string_lossy().to_string())
.chain(self.db.module_files().into_iter())
.filter(|file| file != &current_file)
.collect::<Vec<String>>();

check(
&[current_file.into().0],
&[current_file],
&deps,
self.db.config.dialect().as_ref(),
self.db.config.sender(),
Expand All @@ -58,12 +57,12 @@ impl Analysis {
})
}

fn read_stdlib_files(&self) -> Vec<MoveFile<'static, 'static>> {
fn read_stdlib_files(&self) -> Vec<PathBuf> {
self.db
.config
.stdlib_folder
.as_ref()
.map(|folder| file::load_move_files(&[folder]).unwrap_or_default())
.map(|folder| file::find_move_files(&[folder]).collect())
.unwrap_or_default()
}
}
27 changes: 17 additions & 10 deletions language_server/src/inner/change.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
use core::fmt;
use std::cmp::min;
use crate::inner::config::Config;
use std::fs::read_to_string;

pub enum RootChange {
AddFile { path: String, text: String },
ChangeFile { path: String, text: String },
AddFile { path: String },
ChangeFile { path: String },
RemoveFile { path: String },
}

impl fmt::Debug for RootChange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn text(path: &str) -> String {
read_to_string(&path)
.map(|text| { &text[0..min(text.len(), 55)] }.to_string())
.unwrap_or_else(|_| "file not found".to_string())
}

let mut s = f.debug_struct("RootChange");
match self {
RootChange::AddFile { path, text } => s
RootChange::AddFile { path } => s
.field("fpath", path)
.field("type", &String::from("AddFile"))
.field("text", &text[0..min(text.len(), 55)].to_owned()),
.field("text", &text(path)),
RootChange::RemoveFile { path } => s
.field("fpath", path)
.field("type", &String::from("RemoveFile")),
RootChange::ChangeFile { path, text } => s
RootChange::ChangeFile { path } => s
.field("fpath", path)
.field("type", &String::from("ChangeFile"))
.field("text", &text[0..min(text.len(), 55)].to_owned()),
.field("text", &text(path)),
};
s.finish()
}
Expand All @@ -39,14 +46,14 @@ impl AnalysisChange {
AnalysisChange::default()
}

pub fn add_file(&mut self, fname: String, text: String) {
pub fn add_file(&mut self, fname: String) {
self.tracked_files_changed
.push(RootChange::AddFile { path: fname, text });
.push(RootChange::AddFile { path: fname });
}

pub fn update_file(&mut self, fname: String, text: String) {
pub fn update_file(&mut self, fname: String) {
self.tracked_files_changed
.push(RootChange::ChangeFile { path: fname, text });
.push(RootChange::ChangeFile { path: fname });
}

pub fn remove_file(&mut self, fname: String) {
Expand Down
25 changes: 14 additions & 11 deletions language_server/src/inner/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use move_ir_types::location::Loc;
use std::fmt;
use crate::inner::config::Config;
use crate::inner::change::{AnalysisChange, RootChange};
use std::collections::HashMap;
use std::collections::HashSet;
use lang::compiler::location::File;
use std::fs::read_to_string;

pub struct FileDiagnostic {
pub fpath: String,
Expand Down Expand Up @@ -61,7 +62,7 @@ pub struct FilePosition {
#[derive(Debug, Default, Clone)]
pub struct RootDatabase {
pub config: Config,
pub available_files: HashMap<String, String>,
pub available_files: HashSet<String>,
}

impl RootDatabase {
Expand All @@ -72,11 +73,11 @@ impl RootDatabase {
}
}

pub fn module_files(&self) -> HashMap<String, String> {
pub fn module_files(&self) -> HashSet<String> {
self.available_files
.clone()
.into_iter()
.filter(|(f, _)| self.is_fpath_for_a_module(f))
.filter(|f| self.is_fpath_for_a_module(f))
.collect()
}

Expand All @@ -86,14 +87,14 @@ impl RootDatabase {
}
for root_change in change.tracked_files_changed {
match root_change {
RootChange::AddFile { path, text } => {
self.available_files.insert(path, text);
RootChange::AddFile { path } => {
self.available_files.insert(path);
}
RootChange::ChangeFile { path, text } => {
self.available_files.insert(path, text);
RootChange::ChangeFile { path } => {
self.available_files.insert(path);
}
RootChange::RemoveFile { path } => {
if !self.available_files.contains_key(&path) {
if !self.available_files.contains(&path) {
log::warn!("RemoveFile: file {:?} does not exist", path);
}
self.available_files.remove(&path);
Expand All @@ -104,16 +105,18 @@ impl RootDatabase {

fn loc_to_range(&self, loc: &Loc) -> Result<Range> {
let file = loc.file();

let text = match self.available_files.get(file) {
Some(text) => text.clone(),
Some(text) => read_to_string(text)?,
None => {
anyhow::bail!(
"File {:?} is not present in the available files {:#?}",
file,
&self.available_files.keys()
&self.available_files
);
}
};

let file = File::new(text);
let start_pos = file.position(loc.span().start())?;
let end_pos = file.position(loc.span().end())?;
Expand Down
Loading

0 comments on commit 9b4697a

Please sign in to comment.