Skip to content

Commit

Permalink
fix: regression; Single mode is_opened_in_workspace incorrectly set
Browse files Browse the repository at this point in the history
  • Loading branch information
rszyma committed Jan 31, 2024
1 parent 2f79d90 commit 714ae52
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
25 changes: 20 additions & 5 deletions kls/src/formatter/defsrc_layout/get_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn get_defsrc_layout(
tree: &ExtParseTree, // of current file
) -> anyhow::Result<Option<Vec<Vec<usize>>>> {
match workspace_options {
WorkspaceOptions::Single => {
WorkspaceOptions::Single { .. } => {
if tree.includes()?.is_empty() {
tree.defsrc_layout(tab_size)
} else {
Expand Down Expand Up @@ -113,10 +113,10 @@ mod tests {
fn single_without_includes() {
let src = "(defsrc 1 2) (deflayer base 3 4)";
let layout = get_defsrc_layout(
&WorkspaceOptions::Single,
&WorkspaceOptions::Single { root: None },
&BTreeMap::new(),
4,
&Url::from_str(&format!("file://{MAIN_FILE}")).unwrap(),
&Url::from_str(&format!("file:///{MAIN_FILE}")).unwrap(),
&parse_into_ext_tree_and_root_span(src).unwrap().0,
)
.unwrap()
Expand All @@ -129,14 +129,29 @@ mod tests {
#[test]
fn single_with_includes() {
let src = "(defsrc 1 2) (deflayer base 3 4) (include file.kbd)";

let _ = get_defsrc_layout(
&WorkspaceOptions::Single,
&WorkspaceOptions::Single { root: None },
&BTreeMap::new(),
4,
&Url::from_str(&format!("file://{MAIN_FILE}")).unwrap(),
&Url::from_str(&format!("file:///{MAIN_FILE}")).unwrap(),
&parse_into_ext_tree_and_root_span(src).unwrap().0,
)
.expect_err("should be error, because includes don't work in Single mode");

let _ = get_defsrc_layout(
&WorkspaceOptions::Single {
root: Some(Url::from_str("file:///").unwrap()),
},
&BTreeMap::new(),
4,
&Url::from_str(&format!("file:///{MAIN_FILE}")).unwrap(),
&parse_into_ext_tree_and_root_span(src).unwrap().0,
)
.expect_err(
"should be error, because includes don't work in Single mode,\
even if opened in workspace",
);
}

#[test]
Expand Down
20 changes: 13 additions & 7 deletions kls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,20 @@ impl Display for DefLocalKeysVariant {

#[derive(Debug, Clone)]
enum WorkspaceOptions {
Single,
Workspace { main_config_file: String, root: Url },
/// `root` is `None` in Single mode when file is not opened in a workspace.
Single {
root: Option<Url>,
},
Workspace {
main_config_file: String,
root: Url,
},
}

impl WorkspaceOptions {
fn from_config(config: Config, root_folder: Option<Url>) -> Self {
match config.includes_and_workspaces {
IncludesAndWorkspaces::Single => WorkspaceOptions::Single,
IncludesAndWorkspaces::Single => WorkspaceOptions::Single { root: root_folder },
IncludesAndWorkspaces::Workspace => WorkspaceOptions::Workspace {
main_config_file: config.main_config_file,
root: root_folder.expect("root folder should be set in workspace mode"),
Expand Down Expand Up @@ -525,7 +531,7 @@ impl KanataLanguageServer {
let filename = err.span.file_name();
Url::join(root, &filename).map_err(|e| anyhow!(e.to_string()))?
}
WorkspaceOptions::Single => match &self.documents.first_key_value() {
WorkspaceOptions::Single { .. } => match &self.documents.first_key_value() {
Some(entry) => entry.0.to_owned(),
None => bail!("no kanata files are opened"),
},
Expand Down Expand Up @@ -613,9 +619,9 @@ impl KanataLanguageServer {
let main_cfg_filename: PathBuf = path::PathBuf::from_str(url_path_str)
.expect("shoudn't error because it comes from Url");
let main_cfg_text: &str = &doc.text;
let is_opened_in_workspace: bool = match self.workspace_options {
let is_opened_in_workspace: bool = match &self.workspace_options {
WorkspaceOptions::Workspace { .. } => true,
WorkspaceOptions::Single => false,
WorkspaceOptions::Single { root } => root.is_some(),
};
self.kanata
.parse_single_file(&main_cfg_filename, main_cfg_text, is_opened_in_workspace)
Expand Down Expand Up @@ -651,7 +657,7 @@ impl KanataLanguageServer {
let docs: Vec<_> = docs.iter().collect();

let parse_errors = match &self.workspace_options {
WorkspaceOptions::Single => {
WorkspaceOptions::Single { .. } => {
let results: Vec<_> = docs
.iter()
.filter_map(|doc| self.parse_a_single_file_in_workspace(doc))
Expand Down

0 comments on commit 714ae52

Please sign in to comment.