Skip to content

Commit

Permalink
Enable ignoring files that are not part of the project (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schottkyc137 committed Apr 27, 2024
1 parent e153879 commit ed62c23
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
1 change: 1 addition & 0 deletions vhdl_ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ fn main() {
vhdl_ls::start(VHDLServerSettings {
no_lint: args.no_lint,
silent: args.silent,
..Default::default()
});
}
63 changes: 55 additions & 8 deletions vhdl_ls/src/vhdl_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::collections::HashMap;
use vhdl_lang::ast::{Designator, ObjectClass};

use crate::rpc_channel::SharedRpcChannel;
use serde_json::Value;
use std::io;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
Expand All @@ -21,10 +22,33 @@ use vhdl_lang::{
Source, SrcPos, Token, Type, VHDLStandard,
};

/// Defines how the language server handles files
/// that are not part of the `vhdl_ls.toml` project settings file.
#[derive(Default, Clone, Eq, PartialEq)]
pub enum NonProjectFileHandling {
/// Ignore any non-project files
Ignore,
/// Add non-project files to an anonymous library and analyze them
#[default]
Analyze,
}

impl NonProjectFileHandling {
pub fn from_string(value: &str) -> Option<NonProjectFileHandling> {
use NonProjectFileHandling::*;
Some(match value {
"ignore" => Ignore,
"analyze" => Analyze,
_ => return None,
})
}
}

#[derive(Default, Clone)]
pub struct VHDLServerSettings {
pub no_lint: bool,
pub silent: bool,
pub non_project_file_handling: NonProjectFileHandling,
}

pub struct VHDLServer {
Expand Down Expand Up @@ -116,12 +140,30 @@ impl VHDLServer {
config
}

fn apply_initial_options(&mut self, options: &Value) {
let Some(non_project_file_handling) = options.get("nonProjectFiles") else {
return;
};
match non_project_file_handling {
Value::String(handling) => match NonProjectFileHandling::from_string(handling) {
None => self.message(Message::error(format!(
"Illegal setting {handling} for nonProjectFiles setting"
))),
Some(handling) => self.settings.non_project_file_handling = handling,
},
_ => self.message(Message::error("nonProjectFiles must be a string")),
}
}

pub fn initialize_request(&mut self, init_params: InitializeParams) -> InitializeResult {
self.config_file = self.root_uri_config_file(&init_params);
let config = self.load_config();
self.severity_map = *config.severities();
self.project = Project::from_config(config, &mut self.message_filter());
self.project.enable_unused_declaration_detection();
if let Some(options) = &init_params.initialization_options {
self.apply_initial_options(options)
}
self.init_params = Some(init_params);
let trigger_chars: Vec<String> = r".".chars().map(|ch| ch.to_string()).collect();

Expand Down Expand Up @@ -228,7 +270,7 @@ impl VHDLServer {
}
self.project.update_source(&source);
self.publish_diagnostics();
} else {
} else if self.settings.non_project_file_handling != NonProjectFileHandling::Ignore {
self.message(Message::error(format!(
"Changing file {} that is not part of the project",
file_name.to_string_lossy()
Expand All @@ -244,13 +286,18 @@ impl VHDLServer {
self.project.update_source(&source);
self.publish_diagnostics();
} else {
self.message(Message::warning(format!(
"Opening file {} that is not part of the project",
file_name.to_string_lossy()
)));
self.project
.update_source(&Source::inline(&file_name, text));
self.publish_diagnostics();
match self.settings.non_project_file_handling {
NonProjectFileHandling::Ignore => {}
NonProjectFileHandling::Analyze => {
self.message(Message::warning(format!(
"Opening file {} that is not part of the project",
file_name.to_string_lossy()
)));
self.project
.update_source(&Source::inline(&file_name, text));
self.publish_diagnostics();
}
}
}
}

Expand Down

0 comments on commit ed62c23

Please sign in to comment.