Skip to content

Commit

Permalink
Run the LSP via forc (FuelLabs#485)
Browse files Browse the repository at this point in the history
* Run the LSP via forc

* fmt

* rename common to sway-utils

* alphabetize deps

* forgotten add

* fmt

Co-authored-by: John Adler <[email protected]>
  • Loading branch information
sezna and adlerjohn authored Dec 24, 2021
1 parent 42f96d7 commit 98f3c36
Show file tree
Hide file tree
Showing 26 changed files with 128 additions and 81 deletions.
8 changes: 7 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ members = [
"formatter",
"parser",
"sway-server",
"test"]
"sway-utils",
"test"
]

[profile.dev.package.sway-server]
debug = 2
4 changes: 3 additions & 1 deletion forc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description = "Fuel Orchestrator."
annotate-snippets = { version = "0.9", features = ["color"] } # used for formating error/warning print
anyhow = "1.0.41"
core_lang = { path = "../core_lang" }
core-types = { path = "../core-types" }
curl = "0.4.38"
dirs = "3.0.2"
flate2 = "1.0.20"
Expand All @@ -30,13 +31,14 @@ serde = {version = "1.0", features = ["derive"]}
serde_json = "*"
source-span = "2.4"
structopt = "0.3"
sway-server = { path = "../sway-server" }
sway-utils = { path = "../sway-utils" }
tar = "0.4.35"
term-table = "1.3"
termcolor = "1.1"
tokio = { version = "1.8.0", features = ["macros", "rt-multi-thread", "process"] }
toml = "0.5"
whoami = "1.1"
core-types = { path = "../core-types" }

[features]
default = []
Expand Down
13 changes: 13 additions & 0 deletions forc/src/cli/commands/lsp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use structopt::StructOpt;
use sway_server::start;
/// Run the LSP server.
#[derive(Debug, StructOpt)]
pub(crate) struct Command {}

pub(crate) async fn exec(_command: Command) -> Result<(), String> {
println!(
"Running the {} language server...",
sway_utils::constants::LANGUAGE_NAME
);
Ok(start().await)
}
1 change: 1 addition & 0 deletions forc/src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod deploy;
pub mod format;
pub mod init;
pub mod json_abi;
pub mod lsp;
pub mod parse_bytecode;
pub mod run;
pub mod test;
Expand Down
7 changes: 6 additions & 1 deletion forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use structopt::StructOpt;

mod commands;
use self::commands::{build, deploy, format, init, json_abi, parse_bytecode, run, test, update};
use self::commands::{
build, deploy, format, init, json_abi, lsp, parse_bytecode, run, test, update,
};

pub use build::Command as BuildCommand;
pub use deploy::Command as DeployCommand;
pub use format::Command as FormatCommand;
use init::Command as InitCommand;
pub use json_abi::Command as JsonAbiCommand;
use lsp::Command as LspCommand;
use parse_bytecode::Command as ParseBytecodeCommand;
pub use run::Command as RunCommand;
use test::Command as TestCommand;
Expand All @@ -33,6 +36,7 @@ enum Forc {
Test(TestCommand),
Update(UpdateCommand),
JsonAbi(JsonAbiCommand),
Lsp(LspCommand),
}

pub(crate) async fn run_cli() -> Result<(), String> {
Expand All @@ -47,6 +51,7 @@ pub(crate) async fn run_cli() -> Result<(), String> {
Forc::Test(command) => test::exec(command),
Forc::Update(command) => update::exec(command).await,
Forc::JsonAbi(command) => json_abi::exec(command),
Forc::Lsp(command) => lsp::exec(command).await,
}?;

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions forc/src/ops/forc_abi_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use crate::{
cli::JsonAbiCommand,
utils::dependency,
utils::helpers::{
find_file_name, find_main_path, find_manifest_dir, get_main_file, print_on_failure,
print_on_success, read_manifest,
find_file_name, find_main_path, get_main_file, print_on_failure, print_on_success,
read_manifest,
},
};

use core_types::{Function, JsonABI};
use sway_utils::find_manifest_dir;

use anyhow::Result;
use core_lang::{BuildConfig, CompileAstResult, Namespace, TreeType, TypedParseTree};
Expand Down
9 changes: 4 additions & 5 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use crate::{
cli::BuildCommand,
utils::dependency,
utils::helpers::{
find_manifest_dir, get_main_file, print_on_failure, print_on_success,
print_on_success_library, read_manifest,
get_main_file, print_on_failure, print_on_success, print_on_success_library, read_manifest,
},
};
use core_lang::{FinalizedAsm, TreeType};

use std::fs::File;
use std::io::Write;
use sway_utils::{constants, find_manifest_dir};

use core_lang::{BuildConfig, BytecodeCompilationResult, CompilationResult, Namespace};

Expand Down Expand Up @@ -48,7 +47,7 @@ pub fn build(command: BuildCommand) -> Result<Vec<u8>, String> {

let main_path = {
let mut code_dir = manifest_dir.clone();
code_dir.push(crate::utils::constants::SRC_DIR);
code_dir.push(constants::SRC_DIR);
code_dir.push(&manifest.project.entry);
code_dir
};
Expand Down Expand Up @@ -185,7 +184,7 @@ fn compile_dependency_lib<'n, 'source, 'manifest>(

let main_path = {
let mut code_dir = manifest_dir.clone();
code_dir.push(crate::utils::constants::SRC_DIR);
code_dir.push(constants::SRC_DIR);
code_dir.push(&manifest_of_dep.project.entry);
code_dir
};
Expand Down
7 changes: 2 additions & 5 deletions forc/src/ops/forc_dep_check.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use crate::utils::{
dependency,
helpers::{find_manifest_dir, read_manifest},
};

use crate::utils::{dependency, helpers::read_manifest};
use anyhow::{anyhow, Result};
use dirs::home_dir;
use semver::Version;
use std::{
path::{Path, PathBuf},
str,
};
use sway_utils::find_manifest_dir;

/// Forc check will check if there are updates to Github-based dependencies.
/// If a target dependency `-d` is passed, it will check only this one dependency.
Expand Down
6 changes: 3 additions & 3 deletions forc/src/ops/forc_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use crate::cli::{BuildCommand, DeployCommand};
use crate::ops::forc_build;
use crate::utils::cli_error::CliError;

use crate::utils::{constants, helpers};
use constants::{DEFAULT_NODE_URL, SWAY_CONTRACT, SWAY_LIBRARY, SWAY_PREDICATE, SWAY_SCRIPT};
use helpers::{find_manifest_dir, get_main_file, read_manifest};
use crate::utils::helpers;
use helpers::{get_main_file, read_manifest};
use std::path::PathBuf;
use sway_utils::{constants::*, find_manifest_dir};

pub async fn deploy(command: DeployCommand) -> Result<(), CliError> {
let curr_dir = if let Some(ref path) = command.path {
Expand Down
3 changes: 2 additions & 1 deletion forc/src/ops/forc_fmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::cli::{BuildCommand, FormatCommand};
use crate::ops::forc_build;
use crate::utils::helpers::{find_manifest_dir, get_sway_files, println_green, println_red};
use crate::utils::helpers::{println_green, println_red};
use formatter::get_formatted_data;
use prettydiff::{basic::DiffOp, diff_lines};
use std::{fmt, fs, io, path::Path};
use sway_utils::{find_manifest_dir, get_sway_files};

pub fn format(command: FormatCommand) -> Result<(), FormatError> {
let build_command = BuildCommand {
Expand Down
3 changes: 2 additions & 1 deletion forc/src/ops/forc_init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::{constants, defaults};
use crate::utils::defaults;
use std::fs;
use sway_utils::constants;

pub(crate) fn init_new_project(project_name: String) -> Result<(), Box<dyn std::error::Error>> {
// Make a new directory for the project
Expand Down
6 changes: 3 additions & 3 deletions forc/src/ops/forc_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::ops::forc_build;
use crate::utils::cli_error::CliError;
use crate::utils::client::start_fuel_core;

use crate::utils::{constants, helpers};
use constants::{SWAY_CONTRACT, SWAY_LIBRARY, SWAY_PREDICATE, SWAY_SCRIPT};
use helpers::{find_manifest_dir, get_main_file, read_manifest};
use crate::utils::helpers;
use helpers::{get_main_file, read_manifest};
use sway_utils::{constants::*, find_manifest_dir};

pub async fn run(command: RunCommand) -> Result<(), CliError> {
let path_dir = if let Some(path) = &command.path {
Expand Down
10 changes: 5 additions & 5 deletions forc/src/ops/forc_update.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::utils::constants;
use crate::{
cli::UpdateCommand,
ops::forc_dep_check,
utils::{
dependency,
helpers::{find_manifest_dir, read_manifest},
},
utils::{dependency, helpers::read_manifest},
};
use anyhow::{anyhow, Result};
use dirs::home_dir;
use std::{path::PathBuf, str};
use sway_utils::{
constants::{self},
find_manifest_dir,
};

/// Forc update will update the contents inside the Forc dependencies directory.
/// If a dependency `d` is passed as parameter, it will only try and update that specific dependency.
Expand Down
2 changes: 1 addition & 1 deletion forc/src/utils/cli_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::net::AddrParseError;
use std::path::PathBuf;
use std::{fmt, io};

use super::constants::MANIFEST_FILE_NAME;
use core_lang::CompileError;
use sway_utils::constants::MANIFEST_FILE_NAME;

#[derive(Debug)]
pub struct CliError {
Expand Down
2 changes: 1 addition & 1 deletion forc/src/utils/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::utils::constants;
use crate::utils::manifest::Manifest;
use anyhow::{anyhow, bail, Context, Result};
use curl::easy::Easy;
Expand All @@ -11,6 +10,7 @@ use std::{
io::Cursor,
path::{Path, PathBuf},
};
use sway_utils::constants;
use tar::Archive;

// A collection of remote dependency related functions
Expand Down
50 changes: 6 additions & 44 deletions forc/src/utils/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::constants::{SRC_DIR, SWAY_EXTENSION};
use super::manifest::Manifest;
use annotate_snippets::{
display_list::{DisplayList, FormatOptions},
Expand All @@ -8,55 +7,18 @@ use core_lang::{CompileError, CompileWarning, TreeType};
use std::ffi::OsStr;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::{fs, str};
use std::str;
use sway_utils::constants;
use termcolor::{self, Color as TermColor, ColorChoice, ColorSpec, StandardStream, WriteColor};

pub fn is_sway_file(file: &Path) -> bool {
let res = file.extension();
Some(OsStr::new(SWAY_EXTENSION)) == res
}

pub fn get_sway_files(path: PathBuf) -> Vec<PathBuf> {
let mut files = vec![];
let mut dir_entries = vec![path];

while let Some(next_dir) = dir_entries.pop() {
if let Ok(read_dir) = fs::read_dir(next_dir) {
for entry in read_dir.filter_map(|res| res.ok()) {
let path = entry.path();

if path.is_dir() {
dir_entries.push(path);
} else if is_sway_file(&path) {
files.push(path)
}
}
}
}

files
}

// Continually go up in the file tree until a manifest (Forc.toml) is found.
pub fn find_manifest_dir(starter_path: &Path) -> Option<PathBuf> {
let mut path = std::fs::canonicalize(starter_path).ok()?;
let empty_path = PathBuf::from("/");
while path != empty_path {
path.push(crate::utils::constants::MANIFEST_FILE_NAME);
if path.exists() {
path.pop();
return Some(path);
} else {
path.pop();
path.pop();
}
}
None
Some(OsStr::new(constants::SWAY_EXTENSION)) == res
}

pub fn find_main_path(manifest_dir: &PathBuf, manifest: &Manifest) -> PathBuf {
let mut code_dir = manifest_dir.clone();
code_dir.push(crate::utils::constants::SRC_DIR);
code_dir.push(constants::SRC_DIR);
code_dir.push(&manifest.project.entry);
code_dir
}
Expand All @@ -77,7 +39,7 @@ pub fn find_file_name<'sc>(
pub fn read_manifest(manifest_dir: &Path) -> Result<Manifest, String> {
let manifest_path = {
let mut man = PathBuf::from(manifest_dir);
man.push(crate::utils::constants::MANIFEST_FILE_NAME);
man.push(constants::MANIFEST_FILE_NAME);
man
};
let manifest_path_str = format!("{:?}", manifest_path);
Expand All @@ -102,7 +64,7 @@ pub fn get_main_file(
) -> Result<&'static mut String, String> {
let main_path = {
let mut code_dir = PathBuf::from(manifest_dir);
code_dir.push(SRC_DIR);
code_dir.push(constants::SRC_DIR);
code_dir.push(&manifest_of_dep.project.entry);
code_dir
};
Expand Down
2 changes: 1 addition & 1 deletion forc/src/utils/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::convert::TryFrom;

use super::constants::DEFAULT_NODE_URL;
use sway_utils::constants::DEFAULT_NODE_URL;

// using https://github.com/rust-lang/cargo/blob/master/src/cargo/util/toml/mod.rs as the source of
// implementation strategy
Expand Down
1 change: 0 additions & 1 deletion forc/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod cli_error;
pub mod client;
pub mod constants;
pub mod defaults;
pub mod dependency;
pub mod helpers;
Expand Down
2 changes: 1 addition & 1 deletion sway-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ description = "LSP server for Sway."
[dependencies]
core_lang = { path = "../core_lang" }
dashmap = "4.0.2"
forc = { path = "../forc", features = ["util"], default-features = false }
formatter = { path = "../formatter" }
lspower = "1.0.0"
pest = "2.0"
ropey = "1.2"
serde_json = "1.0.60"
sway-utils = { path = "../sway-utils" }
tokio = { version = "1.3", features = ["io-std", "io-util", "macros", "net", "rt-multi-thread", "sync", "time"] }
Loading

0 comments on commit 98f3c36

Please sign in to comment.