-
Notifications
You must be signed in to change notification settings - Fork 90
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
First prototype of evaluation #1672
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use lsp_server::{RequestId, Response, ResponseError}; | ||
use lsp_types::{CodeActionOrCommand, CodeActionParams}; | ||
|
||
use crate::{cache::CacheExt, server::Server}; | ||
|
||
pub fn handle_code_action( | ||
params: CodeActionParams, | ||
req: RequestId, | ||
server: &mut Server, | ||
) -> Result<(), ResponseError> { | ||
let mut actions = Vec::new(); | ||
|
||
if server.cache.file_id(¶ms.text_document.uri)?.is_some() { | ||
actions.push(CodeActionOrCommand::Command(lsp_types::Command { | ||
title: "evaluate term".to_owned(), | ||
command: "eval".to_owned(), | ||
arguments: Some(vec![serde_json::to_value(¶ms.text_document).unwrap()]), | ||
})); | ||
} | ||
|
||
server.reply(Response::new_ok(req, Some(actions))); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use lsp_server::{RequestId, Response, ResponseError}; | ||
use lsp_types::{ExecuteCommandParams, TextDocumentIdentifier, Url}; | ||
use nickel_lang_core::{ | ||
error::IntoDiagnostics, | ||
eval::{cache::CacheImpl, VirtualMachine}, | ||
}; | ||
|
||
use crate::{cache::CacheExt, error::Error, server::Server}; | ||
|
||
pub fn handle_command( | ||
params: ExecuteCommandParams, | ||
req: RequestId, | ||
server: &mut Server, | ||
) -> Result<(), ResponseError> { | ||
match params.command.as_str() { | ||
"eval" => { | ||
server.reply(Response::new_ok(req, None::<()>)); | ||
|
||
let doc: TextDocumentIdentifier = | ||
serde_json::from_value(params.arguments[0].clone()).unwrap(); | ||
eval(server, &doc.uri)?; | ||
Ok(()) | ||
} | ||
_ => Err(Error::CommandNotFound(params.command).into()), | ||
} | ||
} | ||
|
||
fn eval(server: &mut Server, uri: &Url) -> Result<(), Error> { | ||
if let Some(file_id) = server.cache.file_id(uri)? { | ||
// TODO: avoid cloning the cache. Maybe we can have a VM with a &mut ImportResolver? | ||
let mut vm = VirtualMachine::<_, CacheImpl>::new(server.cache.clone(), std::io::stderr()); | ||
let rt = vm.prepare_eval(file_id)?; | ||
if let Err(e) = vm.eval_full(rt) { | ||
let diags = e.into_diagnostics(server.cache.files_mut(), None); | ||
server.issue_diagnostics(file_id, diags); | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this would be annoying because we'd have to add a lifetime parameter to
VirtualMachine
. I wonder if you can't just implementImportResolver
for&'a mut R
whenR: ImportResolver
, thus passing a mutable reference instead? Or just wrap it as a dedicated typeImportResolverRef<'a, R: ImportResolver>(&'a mut ImportResolver)
and implement the trait. After allVirtualMachine
is generic over the resolver.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's a good point. I'll give it a quick try, and if it works I'll add it to this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementing
ImportResolver
for&'a mut R
worked nicely, but it didn't solve the issue becauseprepare_eval
is only defined when the import resolver parameter isImportCache
. I'll just leave the TODO for now...