Compare commits

...

11 Commits

Author SHA1 Message Date
Denys Rybalka
41be45c6d4
Merge 68b252a5fd into f305c7299d 2024-11-21 18:59:32 +01:00
Lens0021 / Leslie
f305c7299d
Add support for Amber-lang (#12021)
Co-authored-by: Phoenix Himself <pkaras.it@gmail.com>
Co-authored-by: Michael Davis <mcarsondavis@gmail.com>
2024-11-21 10:09:42 -06:00
Valentin B.
9e0d2d0a19
chore(solidity): add highlight queries (#12102)
Add highlights for `hex` and `unicode` string prefixes and YUL booleans
2024-11-21 07:58:14 -06:00
Denys Rybalka
68b252a5fd
Do not resolve symlinks in file browser 2024-10-04 11:47:40 +02:00
Denys Rybalka
adc72f2d87
Distinguish dirs visually in file_browser 2024-10-04 11:32:26 +02:00
Denys Rybalka
7e58404172
Open file browser in cwd when no buffer path 2024-10-04 11:32:26 +02:00
Denys Rybalka
f4fb8fa4ef
Open file browser in buffer's directory 2024-10-04 11:32:26 +02:00
Denys Rybalka
f23e982338
Add parent folder to file browser 2024-10-04 11:32:26 +02:00
Denys Rybalka
8f5e4dff29
Pass directory content into picker directly 2024-10-04 11:32:26 +02:00
Denys Rybalka
084d1811ea
Implement opening of folders 2024-10-04 11:32:25 +02:00
Denys Rybalka
7b0f7dbfbe
Add file browser 2024-10-03 07:59:40 +02:00
7 changed files with 246 additions and 27 deletions

View File

@ -3,6 +3,7 @@
| ada | ✓ | ✓ | | `ada_language_server` |
| adl | ✓ | ✓ | ✓ | |
| agda | ✓ | | | |
| amber | ✓ | | | |
| astro | ✓ | | | |
| awk | ✓ | ✓ | | `awk-language-server` |
| bash | ✓ | ✓ | ✓ | `bash-language-server` |

View File

@ -375,6 +375,7 @@ pub fn doc(&self) -> &str {
file_picker, "Open file picker",
file_picker_in_current_buffer_directory, "Open file picker at current buffer's directory",
file_picker_in_current_directory, "Open file picker at current working directory",
file_browser, "Open file browser at current buffer's directory",
code_action, "Perform code action",
buffer_picker, "Open buffer picker",
jumplist_picker, "Open jumplist picker",
@ -2950,6 +2951,30 @@ fn file_picker_in_current_directory(cx: &mut Context) {
cx.push_layer(Box::new(overlaid(picker)));
}
fn file_browser(cx: &mut Context) {
let doc_dir = doc!(cx.editor)
.path()
.and_then(|path| path.parent().map(|path| path.to_path_buf()));
let path = match doc_dir {
Some(path) => path,
None => {
let cwd = helix_stdx::env::current_working_dir();
if !cwd.exists() {
cx.editor.set_error(
"Current buffer has no parent and current working directory does not exist",
);
return;
}
cwd
}
};
if let Ok(picker) = ui::file_browser(path) {
cx.push_layer(Box::new(overlaid(picker)));
}
}
fn buffer_picker(cx: &mut Context) {
let current = view!(cx.editor).doc;

View File

@ -30,6 +30,7 @@
use helix_view::Editor;
use std::path::Path;
use std::{error::Error, path::PathBuf};
pub fn prompt(
@ -265,6 +266,76 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
picker
}
pub fn file_browser(root: PathBuf) -> Result<FilePicker, std::io::Error> {
let root = helix_stdx::path::canonicalize(root);
let directory_content = directory_content(&root)?;
let columns = [PickerColumn::new(
"path",
|item: &PathBuf, root: &PathBuf| {
let name = item.strip_prefix(root).unwrap_or(item).to_string_lossy();
if item.is_dir() {
format!("{}/", name).into()
} else {
name.into()
}
},
)];
let picker = Picker::new(
columns,
0,
directory_content,
root,
move |cx, path: &PathBuf, action| {
if path.is_dir() {
let owned_path = path.clone();
let callback = Box::pin(async move {
let call: Callback =
Callback::EditorCompositor(Box::new(move |_editor, compositor| {
if let Ok(picker) = file_browser(owned_path) {
compositor.push(Box::new(overlay::overlaid(picker)));
}
}));
Ok(call)
});
cx.jobs.callback(callback);
} else if let Err(e) = cx.editor.open(path, action) {
let err = if let Some(err) = e.source() {
format!("{}", err)
} else {
format!("unable to open \"{}\"", path.display())
};
cx.editor.set_error(err);
}
},
)
.with_preview(|_editor, path| Some((path.as_path().into(), None)));
Ok(picker)
}
fn directory_content(path: &Path) -> Result<Vec<PathBuf>, std::io::Error> {
let mut dirs = Vec::new();
let mut files = Vec::new();
for entry in std::fs::read_dir(path)?.flatten() {
if entry.path().is_dir() {
dirs.push(entry.path());
} else {
files.push(entry.path());
}
}
dirs.sort();
files.sort();
let mut content = Vec::new();
if path.parent().is_some() {
content.insert(0, path.join(".."));
}
content.extend(dirs);
content.extend(files);
Ok(content)
}
pub mod completers {
use crate::ui::prompt::Completion;
use helix_core::fuzzy::fuzzy_match;

View File

@ -85,6 +85,7 @@ fn from(v: DocumentId) -> Self {
pub enum CachedPreview {
Document(Box<Document>),
Directory(Vec<String>),
Binary,
LargeFile,
NotFound,
@ -106,12 +107,20 @@ fn document(&self) -> Option<&Document> {
}
}
fn dir_content(&self) -> Option<&Vec<String>> {
match self {
Preview::Cached(CachedPreview::Directory(dir_content)) => Some(dir_content),
_ => None,
}
}
/// Alternate text to show for the preview.
fn placeholder(&self) -> &str {
match *self {
Self::EditorDocument(_) => "<Invalid file location>",
Self::Cached(preview) => match preview {
CachedPreview::Document(_) => "<Invalid file location>",
CachedPreview::Directory(_) => "<Invalid directory location>",
CachedPreview::Binary => "<Binary file>",
CachedPreview::LargeFile => "<File too large to preview>",
CachedPreview::NotFound => "<File not found>",
@ -584,33 +593,58 @@ fn get_preview<'picker, 'editor>(
}
let path: Arc<Path> = path.into();
let data = std::fs::File::open(&path).and_then(|file| {
let metadata = file.metadata()?;
// Read up to 1kb to detect the content type
let n = file.take(1024).read_to_end(&mut self.read_buffer)?;
let content_type = content_inspector::inspect(&self.read_buffer[..n]);
self.read_buffer.clear();
Ok((metadata, content_type))
});
let preview = data
.map(
|(metadata, content_type)| match (metadata.len(), content_type) {
(_, content_inspector::ContentType::BINARY) => CachedPreview::Binary,
(size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => {
CachedPreview::LargeFile
let preview = std::fs::metadata(&path)
.and_then(|metadata| {
if metadata.is_dir() {
let files = super::directory_content(&path)?;
let file_names: Vec<_> = files
.iter()
.filter_map(|file| {
let name = file.file_name()?.to_string_lossy();
if file.is_dir() {
Some(format!("{}/", name))
} else {
Some(name.into_owned())
}
})
.collect();
Ok(CachedPreview::Directory(file_names))
} else if metadata.is_file() {
if metadata.len() > MAX_FILE_SIZE_FOR_PREVIEW {
return Ok(CachedPreview::LargeFile);
}
_ => Document::open(&path, None, None, editor.config.clone())
.map(|doc| {
let content_type = std::fs::File::open(&path).and_then(|file| {
// Read up to 1kb to detect the content type
let n = file.take(1024).read_to_end(&mut self.read_buffer)?;
let content_type =
content_inspector::inspect(&self.read_buffer[..n]);
self.read_buffer.clear();
Ok(content_type)
})?;
if content_type.is_binary() {
return Ok(CachedPreview::Binary);
}
Document::open(&path, None, None, editor.config.clone()).map_or(
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Cannot open document",
)),
|doc| {
// Asynchronously highlight the new document
helix_event::send_blocking(
&self.preview_highlight_handler,
path.clone(),
);
CachedPreview::Document(Box::new(doc))
})
.unwrap_or(CachedPreview::NotFound),
},
)
Ok(CachedPreview::Document(Box::new(doc)))
},
)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Neither a dir, nor a file",
))
}
})
.unwrap_or(CachedPreview::NotFound);
self.preview_cache.insert(path.clone(), preview);
Some((Preview::Cached(&self.preview_cache[&path]), range))
@ -844,6 +878,20 @@ fn render_preview(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context
doc
}
_ => {
if let Some(dir_content) = preview.dir_content() {
for (i, entry) in dir_content.iter().take(inner.height as usize).enumerate()
{
surface.set_stringn(
inner.x,
inner.y + i as u16,
entry,
inner.width as usize,
text,
);
}
return;
}
let alt_text = preview.placeholder();
let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2;
let y = inner.y + inner.height / 2;

View File

@ -3931,3 +3931,14 @@ indent = { tab-width = 4, unit = " " }
[[grammar]]
name = "spade"
source = { git = "https://gitlab.com/spade-lang/tree-sitter-spade/", rev = "4d5b141017c61fe7e168e0a5c5721ee62b0d9572" }
[[language]]
name = "amber"
scope = "source.ab"
file-types = ["ab"]
comment-token = "//"
indent = { tab-width = 4, unit = " " }
[[grammar]]
name = "amber"
source = { git = "https://github.com/amber-lang/tree-sitter-amber", rev = "c6df3ec2ec243ed76550c525e7ac3d9a10c6c814" }

View File

@ -0,0 +1,60 @@
(comment) @comment
[
"if"
"loop"
"for"
"return"
"fun"
"else"
"then"
"break"
"continue"
"and"
"or"
"not"
"let"
"pub"
"main"
"echo"
"exit"
"fun"
"import"
"from"
"as"
"in"
"fail"
"failed"
"silent"
"nameof"
"is"
"unsafe"
"trust"
] @keyword
; Literals
(boolean) @constant.builtin.boolean
(number) @constant.numeric
(null) @constant.numeric
(string) @string
(status) @keyword
(command) @string
(handler) @keyword
(block) @punctuation.delimiter
(variable_init) @keyword
(variable_assignment) @punctuation.delimiter
(variable) @variable
(escape_sequence) @constant.character.escape
(type_name_symbol) @type
(interpolation) @punctuation.delimiter
(reference) @keyword
(preprocessor_directive) @comment
(shebang) @comment
(function_definition
name: (variable) @function.method)
(function_call
name: (variable) @function.method)
(import_statement
"pub" @keyword
"import" @keyword
"from" @keyword)

View File

@ -12,6 +12,8 @@
(unicode_string_literal)
(yul_string_literal)
] @string
(hex_string_literal "hex" @string.special.symbol)
(unicode_string_literal "unicode" @string.special.symbol)
[
(number_literal)
(yul_decimal_number)
@ -20,6 +22,7 @@
[
(true)
(false)
(yul_boolean)
] @constant.builtin.boolean
(comment) @comment
@ -44,18 +47,18 @@
(type_name "(" @punctuation.bracket "=>" @punctuation.delimiter ")" @punctuation.bracket)
; Definitions
(struct_declaration
(struct_declaration
name: (identifier) @type)
(enum_declaration
(enum_declaration
name: (identifier) @type)
(contract_declaration
name: (identifier) @type)
name: (identifier) @type)
(library_declaration
name: (identifier) @type)
name: (identifier) @type)
(interface_declaration
name: (identifier) @type)
(event_definition
name: (identifier) @type)
(event_definition
name: (identifier) @type)
(function_definition
name: (identifier) @function)