Skip to content

Commit

Permalink
Display documentation for sway keywords (FuelLabs#3792)
Browse files Browse the repository at this point in the history
This PR provides documentation when hovering over keywords in sway. I
copied over the documentation for keywords that are shared with rust and
tweaked them so they made sense to sway [from
here](https://github.com/rust-lang/rust/blob/master/library/std/src/keyword_docs.rs
).

It would be great to get some compiler engineers to look over the
documentation to make sure nothing is being incorrectly defined.

There are still a number of keywords that need documentation written up
for them but I've opened up FuelLabs#3939 so these can be added by someone on
the compiler team at a later date.

will close FuelLabs#3515

---------
  • Loading branch information
JoshuaBatty authored Feb 2, 2023
1 parent dd9ab73 commit f7c323f
Show file tree
Hide file tree
Showing 8 changed files with 894 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions sway-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ forc-tracing = { version = "0.34.0", path = "../forc-tracing" }
notify = "5.0.0"
notify-debouncer-mini = { version = "0.2.0" }
parking_lot = "0.12.1"
proc-macro2 = "1.0.5"
quote = "1.0.9"
ropey = "1.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.60"
Expand All @@ -26,6 +28,7 @@ sway-parse = { version = "0.34.0", path = "../sway-parse" }
sway-types = { version = "0.34.0", path = "../sway-types" }
sway-utils = { version = "0.34.0", path = "../sway-utils" }
swayfmt = { version = "0.34.0", path = "../swayfmt" }
syn = { version = "1.0.73", features = ["full"] }
tempfile = "3"
thiserror = "1.0.30"
tokio = { version = "1.3", features = ["io-std", "io-util", "macros", "net", "rt-multi-thread", "sync", "time"] }
Expand Down
28 changes: 25 additions & 3 deletions sway-lsp/src/capabilities/hover.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::{
core::{
session::Session,
token::{get_range_from_span, to_ident_key, Token, TypedAstToken},
token::{get_range_from_span, to_ident_key, SymbolKind, Token, TypedAstToken},
},
utils::{
attributes::doc_comment_attributes, keyword_docs::KeywordDocs, markdown, markup::Markup,
},
utils::{attributes::doc_comment_attributes, markdown, markup::Markup},
};
use std::sync::Arc;
use sway_core::{
Expand All @@ -14,9 +16,29 @@ use sway_types::{Ident, Span, Spanned};
use tower_lsp::lsp_types::{self, Position, Url};

/// Extracts the hover information for a token at the current position.
pub fn hover_data(session: Arc<Session>, url: Url, position: Position) -> Option<lsp_types::Hover> {
pub fn hover_data(
session: Arc<Session>,
keyword_docs: &KeywordDocs,
url: Url,
position: Position,
) -> Option<lsp_types::Hover> {
let (ident, token) = session.token_map().token_at_position(&url, position)?;
let range = get_range_from_span(&ident.span());

// check if our token is a keyword
if token.kind == SymbolKind::Keyword {
let name = ident.as_str();
let documentation = keyword_docs.get(name).unwrap();
let prefix = format!("\n```sway\n{name}\n```\n\n---\n\n");
let formatted_doc = format!("{prefix}{documentation}");
let content = Markup::new().text(&formatted_doc);
let contents = lsp_types::HoverContents::Markup(markup_content(content));
return Some(lsp_types::Hover {
contents,
range: Some(range),
});
}

let (decl_ident, decl_token) = match token.declared_token_ident(&session.type_engine.read()) {
Some(decl_ident) => {
let decl_token = session
Expand Down
12 changes: 10 additions & 2 deletions sway-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
config::{Config, Warnings},
core::{session::Session, sync},
error::{DirectoryError, LanguageServerError},
utils::debug,
utils::{debug, keyword_docs::KeywordDocs},
};
use dashmap::DashMap;
use forc_pkg::manifest::PackageManifestFile;
Expand All @@ -27,17 +27,20 @@ use tracing::metadata::LevelFilter;
pub struct Backend {
pub client: Client,
pub config: RwLock<Config>,
pub keyword_docs: KeywordDocs,
sessions: DashMap<PathBuf, Arc<Session>>,
}

impl Backend {
pub fn new(client: Client) -> Self {
let sessions = DashMap::new();
let config = RwLock::new(Default::default());
let keyword_docs = KeywordDocs::new();

Backend {
client,
config,
keyword_docs,
sessions,
}
}
Expand Down Expand Up @@ -302,7 +305,12 @@ impl LanguageServer for Backend {
match self.get_uri_and_session(&params.text_document_position_params.text_document.uri) {
Ok((uri, session)) => {
let position = params.text_document_position_params.position;
Ok(capabilities::hover::hover_data(session, uri, position))
Ok(capabilities::hover::hover_data(
session,
&self.keyword_docs,
uri,
position,
))
}
Err(err) => {
tracing::error!("{}", err.to_string());
Expand Down
Loading

0 comments on commit f7c323f

Please sign in to comment.