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

print all parsed LSP tokens as warnings in VScode #1148

Merged
merged 8 commits into from
Apr 7, 2022
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions forc/src/cli/commands/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use anyhow::Result;
use crate::ops::forc_lsp;
use anyhow::{bail, Result};
use clap::Parser;

/// Run the LSP server.
#[derive(Debug, Parser)]
pub(crate) struct Command {}
pub struct Command {
/// Instructs the client to draw squiggly lines
/// under all of the tokens that our server managed to parse
#[clap(long)]
pub parsed_tokens_as_warnings: bool,
}

pub(crate) async fn exec(_command: Command) -> Result<()> {
sway_lsp::start().await;
Ok(())
pub(crate) async fn exec(command: Command) -> Result<()> {
match forc_lsp::exec(command).await {
Err(e) => bail!(e),
_ => Ok(()),
}
}
2 changes: 1 addition & 1 deletion forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use explorer::Command as ExplorerCommand;
pub use format::Command as FormatCommand;
pub use init::Command as InitCommand;
pub use json_abi::Command as JsonAbiCommand;
use lsp::Command as LspCommand;
pub use lsp::Command as LspCommand;
use parse_bytecode::Command as ParseBytecodeCommand;
pub use run::Command as RunCommand;
use test::Command as TestCommand;
Expand Down
13 changes: 13 additions & 0 deletions forc/src/ops/forc_lsp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::cli::LspCommand;
use anyhow::Result;
use sway_lsp::utils::debug;

pub async fn exec(command: LspCommand) -> Result<()> {
let config = debug::DebugFlags {
parsed_tokens_as_warnings: command.parsed_tokens_as_warnings,
};

sway_lsp::start(config).await;

Ok(())
}
1 change: 1 addition & 0 deletions forc/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ pub mod forc_deploy;
pub mod forc_explorer;
pub mod forc_fmt;
pub mod forc_init;
pub mod forc_lsp;
pub mod forc_run;
pub mod forc_update;
7 changes: 4 additions & 3 deletions sway-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ mod capabilities;
mod core;
mod server;
mod sway_config;
mod utils;
pub mod utils;
use server::Backend;
use utils::debug::DebugFlags;

pub async fn start() {
pub async fn start(config: DebugFlags) {
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();

let (service, socket) = LspService::new(Backend::new);
let (service, socket) = LspService::new(|client| Backend::new(client, config));
Server::new(stdin, stdout, socket).serve(service).await;
}
45 changes: 33 additions & 12 deletions sway-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::core::{
document::{DocumentError, TextDocument},
session::Session,
};
use crate::utils::debug::{self, DebugFlags};
use forc_util::find_manifest_dir;
use std::sync::Arc;
use sway_utils::helpers::get_sway_files;
Expand All @@ -13,12 +14,17 @@ use tower_lsp::{jsonrpc, Client, LanguageServer};
pub struct Backend {
pub client: Client,
session: Arc<Session>,
config: DebugFlags,
}

impl Backend {
pub fn new(client: Client) -> Self {
pub fn new(client: Client, config: DebugFlags) -> Self {
let session = Arc::new(Session::new());
Backend { client, session }
Backend {
client,
session,
config,
}
}

async fn log_info_message(&self, message: &str) {
Expand Down Expand Up @@ -110,11 +116,22 @@ impl LanguageServer for Backend {

// Document Handlers
async fn did_open(&self, params: DidOpenTextDocumentParams) {
let uri = params.text_document.uri.clone();
let diagnostics = capabilities::text_sync::handle_open_file(self.session.clone(), &params);

if !diagnostics.is_empty() {
// If parsed_tokens_as_warnings is true, take over the normal error and warning display behavior
// and instead show the parsed tokens as warnings.
// This is useful for debugging the lsp parser.
if self.config.parsed_tokens_as_warnings {
if let Some(document) = self.session.documents.get(uri.path()) {
let diagnostics = debug::generate_warnings_for_parsed_tokens(document.get_tokens());
self.client
.publish_diagnostics(uri, diagnostics, None)
.await;
}
} else if !diagnostics.is_empty() {
self.client
.publish_diagnostics(params.text_document.uri, diagnostics, None)
.publish_diagnostics(uri, diagnostics, None)
.await;
}
}
Expand Down Expand Up @@ -337,17 +354,21 @@ fn main() {
assert_eq!(response, Ok(None));
}

fn config() -> DebugFlags {
Default::default()
}

#[tokio::test]
async fn initialize() {
let (mut service, _) = LspService::new(Backend::new);
let (mut service, _) = LspService::new(|client| Backend::new(client, config()));

// send "initialize" request
let _ = initialize_request(&mut service).await;
}

#[tokio::test]
async fn initialized() {
let (mut service, _) = LspService::new(Backend::new);
let (mut service, _) = LspService::new(|client| Backend::new(client, config()));

// send "initialize" request
let _ = initialize_request(&mut service).await;
Expand All @@ -358,7 +379,7 @@ fn main() {

#[tokio::test]
async fn initializes_only_once() {
let (mut service, _) = LspService::new(Backend::new);
let (mut service, _) = LspService::new(|client| Backend::new(client, config()));

// send "initialize" request
let initialize = initialize_request(&mut service).await;
Expand All @@ -374,7 +395,7 @@ fn main() {

#[tokio::test]
async fn shutdown() {
let (mut service, _) = LspService::new(Backend::new);
let (mut service, _) = LspService::new(|client| Backend::new(client, config()));

// send "initialize" request
let _ = initialize_request(&mut service).await;
Expand All @@ -396,7 +417,7 @@ fn main() {

#[tokio::test]
async fn refuses_requests_after_shutdown() {
let (mut service, _) = LspService::new(Backend::new);
let (mut service, _) = LspService::new(|client| Backend::new(client, config()));

// send "initialize" request
let _ = initialize_request(&mut service).await;
Expand All @@ -411,7 +432,7 @@ fn main() {

#[tokio::test]
async fn did_open() {
let (mut service, mut messages) = LspService::new(Backend::new);
let (mut service, mut messages) = LspService::new(|client| Backend::new(client, config()));

// send "initialize" request
let _ = initialize_request(&mut service).await;
Expand All @@ -436,7 +457,7 @@ fn main() {

#[tokio::test]
async fn did_close() {
let (mut service, _) = LspService::new(Backend::new);
let (mut service, _) = LspService::new(|client| Backend::new(client, config()));

// send "initialize" request
let _ = initialize_request(&mut service).await;
Expand All @@ -461,7 +482,7 @@ fn main() {

#[tokio::test]
async fn did_change() {
let (mut service, mut messages) = LspService::new(Backend::new);
let (mut service, mut messages) = LspService::new(|client| Backend::new(client, config()));

// send "initialize" request
let _ = initialize_request(&mut service).await;
Expand Down
24 changes: 24 additions & 0 deletions sway-lsp/src/utils/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::core::token::Token;
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity};

// Flags for debugging various parts of the server
#[derive(Debug, Default)]
pub struct DebugFlags {
/// Instructs the client to draw squiggly lines
/// under all of the tokens that our server managed to parse
pub parsed_tokens_as_warnings: bool,
}

pub fn generate_warnings_for_parsed_tokens(tokens: &[Token]) -> Vec<Diagnostic> {
let warnings = tokens
.iter()
.map(|token| Diagnostic {
range: token.range,
severity: Some(DiagnosticSeverity::WARNING),
message: token.name.clone(),
..Default::default()
})
.collect();

warnings
}
1 change: 1 addition & 0 deletions sway-lsp/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod common;
pub mod debug;
pub(crate) mod function;