Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve imports for incomplete inputs also #1542

Merged
merged 1 commit into from
Aug 25, 2023
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
5 changes: 4 additions & 1 deletion lsp/nls/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ pub fn handle_save(server: &mut Server, params: DidChangeTextDocumentParams) ->
Ok(())
}

fn typecheck(server: &mut Server, file_id: FileId) -> Result<CacheOp<()>, Vec<Diagnostic<FileId>>> {
pub(crate) fn typecheck(
server: &mut Server,
file_id: FileId,
) -> Result<CacheOp<()>, Vec<Diagnostic<FileId>>> {
server
.cache
.typecheck_with_analysis(
Expand Down
23 changes: 21 additions & 2 deletions lsp/nls/src/incomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use nickel_lang_core::{
parser::lexer::{self, NormalToken, SpannedToken, Token},
position::RawSpan,
term::RichTerm,
transform::import_resolution,
};

use crate::server::Server;
use crate::{files::typecheck, server::Server};

// Take a bunch of tokens and the end of a possibly-delimited sequence, and return the
// index of the beginning of the possibly-delimited sequence. The sequence might not
Expand Down Expand Up @@ -90,6 +91,24 @@ fn path_start(toks: &[SpannedToken]) -> Option<usize> {
}
}

fn resolve_imports(rt: RichTerm, server: &mut Server) -> RichTerm {
let import_resolution::tolerant::ResolveResult {
transformed_term,
resolved_ids,
..
} = import_resolution::tolerant::resolve_imports(rt, &mut server.cache);

for id in resolved_ids {
if server.cache.parse(id).is_ok() {
// If a new input got imported in an incomplete term, try to typecheck
// (and build lookup tables etc.) for it, but don't issue diagnostics.
let _ = typecheck(server, id);
}
}

transformed_term
}

/// Given a range of input that we don't expect will fully parse, try to find
/// a record access path at the end of the input, parse it, and return the
/// resulting term.
Expand Down Expand Up @@ -125,7 +144,7 @@ pub fn parse_path_from_incomplete_input(range: RawSpan, server: &mut Server) ->
let file_id = server.cache.replace_string("<incomplete-input>", to_parse);

match server.cache.parse_nocache(file_id) {
Ok((rt, _errors)) => Some(rt),
Ok((rt, _errors)) => Some(resolve_imports(rt, server)),
Err(_) => None,
}
}