Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions components/clarinet-files/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,25 @@ impl fmt::Display for FileLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FileLocation::FileSystem { path } => write!(f, "{}", path.display()),
FileLocation::Url { url } => write!(f, "{}", url),
FileLocation::Url { url } => write!(f, "{url}"),
}
}
}

impl TryInto<Url> for &FileLocation {
type Error = &'static str;

fn try_into(self) -> Result<Url, Self::Error> {
match self {
#[cfg(target_arch = "wasm32")]
FileLocation::FileSystem { .. } => {
Err("Url::from_file_path() not available on target architecture")
}
#[cfg(not(target_arch = "wasm32"))]
FileLocation::FileSystem { path } => {
Url::from_file_path(path).map_err(|()| "Url::from_file_path() failed")
}
FileLocation::Url { url } => Ok(url.clone()),
}
}
}
Expand Down Expand Up @@ -101,13 +119,13 @@ impl FileLocation {

pub fn from_url_string(url_string: &str) -> Result<FileLocation, String> {
let url = Url::from_str(url_string)
.map_err(|e| format!("unable to parse {} as a url\n{:?}", url_string, e))?;
.map_err(|e| format!("unable to parse {url_string} as a url\n{e:?}"))?;

#[cfg(not(target_arch = "wasm32"))]
if url.scheme() == "file" {
let path = url
.to_file_path()
.map_err(|_| format!("unable to conver url {} to path", url))?;
.map_err(|_| format!("unable to conver url {url} to path"))?;
return Ok(FileLocation::FileSystem { path });
}

Expand All @@ -116,13 +134,13 @@ impl FileLocation {

pub fn from_path_string(path_string: &str) -> Result<FileLocation, String> {
let path = PathBuf::from_str(path_string)
.map_err(|e| format!("unable to parse {} as a path\n{:?}", path_string, e))?;
.map_err(|e| format!("unable to parse {path_string} as a path\n{e:?}"))?;
Ok(FileLocation::FileSystem { path })
}

pub fn append_path(&mut self, path_string: &str) -> Result<(), String> {
let path_to_append = PathBuf::from_str(path_string)
.map_err(|e| format!("unable to read relative path {}\n{:?}", path_string, e))?;
.map_err(|e| format!("unable to read relative path {path_string}\n{e:?}"))?;
match self.borrow_mut() {
FileLocation::FileSystem { path } => {
path.extend(&path_to_append);
Expand All @@ -135,7 +153,7 @@ impl FileLocation {
let segment = component
.as_os_str()
.to_str()
.ok_or(format!("unable to format component {:?}", component))?;
.ok_or(format!("unable to format component {component:?}"))?;
paths_segments.push(segment);
}
}
Expand All @@ -146,7 +164,7 @@ impl FileLocation {
pub fn read_content_as_utf8(&self) -> Result<String, String> {
let content = self.read_content()?;
let contract_as_utf8 = String::from_utf8(content)
.map_err(|e| format!("unable to read content as utf8 {}\n{:?}", self, e))?;
.map_err(|e| format!("unable to read content as utf8 {self}\n{e:?}"))?;
Ok(contract_as_utf8)
}

Expand Down
7 changes: 3 additions & 4 deletions components/clarity-lsp/src/common/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use clarity_repl::clarity::{ClarityName, ClarityVersion, StacksEpochId, Symbolic
use clarity_repl::repl::{ContractDeployer, DEFAULT_CLARITY_VERSION};
use lsp_types::{
CompletionItem, DocumentSymbol, Hover, Location, MessageType, Position, Range, SignatureHelp,
Url,
};
use std::borrow::BorrowMut;
use std::collections::{BTreeMap, HashMap, HashSet};
Expand Down Expand Up @@ -365,7 +364,7 @@ impl EditorState {

match definitions.get(&position_hash)? {
DefinitionLocation::Internal(range) => Some(Location {
uri: Url::parse(&contract_location.to_string()).ok()?,
uri: contract_location.try_into().ok()?,
range: *range,
}),
DefinitionLocation::External(contract_identifier, function_name) => {
Expand All @@ -383,18 +382,18 @@ impl EditorState {
{
let public_definitions = get_public_function_definitions(expressions);
return Some(Location {
uri: contract_location.try_into().ok()?,
range: *public_definitions.get(function_name)?,
uri: Url::parse(&definition_contract_location.to_string()).ok()?,
});
};

Some(Location {
uri: contract_location.try_into().ok()?,
range: *protocol
.contracts
.get(definition_contract_location)?
.definitions
.get(function_name)?,
uri: Url::parse(&definition_contract_location.to_string()).ok()?,
})
}
}
Expand Down