mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
Move assists documentation into the manual
This commit is contained in:
parent
5a2f4548e5
commit
46292c7cec
6 changed files with 366 additions and 189 deletions
File diff suppressed because it is too large
Load diff
|
@ -278,5 +278,6 @@ include::./generated_features.adoc[]
|
||||||
|
|
||||||
Assists, or code actions, are small local refactorings, available in a particular context.
|
Assists, or code actions, are small local refactorings, available in a particular context.
|
||||||
They are usually triggered by a shortcut or by clicking a light bulb icon in the editor.
|
They are usually triggered by a shortcut or by clicking a light bulb icon in the editor.
|
||||||
|
Cursor position or selection is signified by `┃` character.
|
||||||
|
|
||||||
See [assists.md](./assists.md) for the list of available assists.
|
include::./generated_assists.adoc[]
|
||||||
|
|
|
@ -10,9 +10,12 @@ mod gen_parser_tests;
|
||||||
mod gen_assists_docs;
|
mod gen_assists_docs;
|
||||||
mod gen_feature_docs;
|
mod gen_feature_docs;
|
||||||
|
|
||||||
use std::{mem, path::Path};
|
use std::{
|
||||||
|
fmt, mem,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{not_bash::fs2, Result};
|
use crate::{not_bash::fs2, project_root, Result};
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
gen_assists_docs::generate_assists_docs, gen_feature_docs::generate_feature_docs,
|
gen_assists_docs::generate_assists_docs, gen_feature_docs::generate_feature_docs,
|
||||||
|
@ -29,7 +32,6 @@ const AST_TOKENS: &str = "crates/ra_syntax/src/ast/generated/tokens.rs";
|
||||||
|
|
||||||
const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers";
|
const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers";
|
||||||
const ASSISTS_TESTS: &str = "crates/ra_assists/src/tests/generated.rs";
|
const ASSISTS_TESTS: &str = "crates/ra_assists/src/tests/generated.rs";
|
||||||
const ASSISTS_DOCS: &str = "docs/user/assists.md";
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
pub enum Mode {
|
pub enum Mode {
|
||||||
|
@ -107,3 +109,28 @@ fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lines: bool) ->
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Location {
|
||||||
|
file: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Location {
|
||||||
|
fn new(file: PathBuf) -> Self {
|
||||||
|
Self { file }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Location {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let path = self.file.strip_prefix(&project_root()).unwrap().display().to_string();
|
||||||
|
let path = path.replace('\\', "/");
|
||||||
|
let name = self.file.file_name().unwrap();
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"https://github.com/rust-analyzer/rust-analyzer/blob/master/{}[{}]",
|
||||||
|
path,
|
||||||
|
name.to_str().unwrap()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,22 +1,28 @@
|
||||||
//! Generates `assists.md` documentation.
|
//! Generates `assists.md` documentation.
|
||||||
|
|
||||||
use std::{fs, path::Path};
|
use std::{fmt, fs, path::Path};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{self, extract_comment_blocks_with_empty_lines, Mode},
|
codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode},
|
||||||
project_root, rust_files, Result,
|
project_root, rust_files, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn generate_assists_docs(mode: Mode) -> Result<()> {
|
pub fn generate_assists_docs(mode: Mode) -> Result<()> {
|
||||||
let assists = Assist::collect()?;
|
let assists = Assist::collect()?;
|
||||||
generate_tests(&assists, mode)?;
|
generate_tests(&assists, mode)?;
|
||||||
generate_docs(&assists, mode)?;
|
|
||||||
|
let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
|
||||||
|
let contents = contents.trim().to_string() + "\n";
|
||||||
|
let dst = project_root().join("docs/user/generated_assists.adoc");
|
||||||
|
codegen::update(&dst, &contents, mode)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Assist {
|
struct Assist {
|
||||||
id: String,
|
id: String,
|
||||||
|
location: Location,
|
||||||
doc: String,
|
doc: String,
|
||||||
before: String,
|
before: String,
|
||||||
after: String,
|
after: String,
|
||||||
|
@ -58,7 +64,8 @@ impl Assist {
|
||||||
assert_eq!(lines.next().unwrap().as_str(), "->");
|
assert_eq!(lines.next().unwrap().as_str(), "->");
|
||||||
assert_eq!(lines.next().unwrap().as_str(), "```");
|
assert_eq!(lines.next().unwrap().as_str(), "```");
|
||||||
let after = take_until(lines.by_ref(), "```");
|
let after = take_until(lines.by_ref(), "```");
|
||||||
acc.push(Assist { id, doc, before, after })
|
let location = Location::new(path.to_path_buf());
|
||||||
|
acc.push(Assist { id, location, doc, before, after })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn take_until<'a>(lines: impl Iterator<Item = &'a String>, marker: &str) -> String {
|
fn take_until<'a>(lines: impl Iterator<Item = &'a String>, marker: &str) -> String {
|
||||||
|
@ -76,6 +83,31 @@ impl Assist {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Assist {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let before = self.before.replace("<|>", "┃"); // Unicode pseudo-graphics bar
|
||||||
|
let after = self.after.replace("<|>", "┃");
|
||||||
|
writeln!(
|
||||||
|
f,
|
||||||
|
"[discrete]\n=== `{}`
|
||||||
|
|
||||||
|
{}
|
||||||
|
|
||||||
|
.Before
|
||||||
|
```rust
|
||||||
|
{}```
|
||||||
|
|
||||||
|
.After
|
||||||
|
```rust
|
||||||
|
{}```",
|
||||||
|
self.id,
|
||||||
|
self.doc,
|
||||||
|
hide_hash_comments(&before),
|
||||||
|
hide_hash_comments(&after)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> {
|
fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> {
|
||||||
let mut buf = String::from("use super::check_doc_test;\n");
|
let mut buf = String::from("use super::check_doc_test;\n");
|
||||||
|
|
||||||
|
@ -103,37 +135,6 @@ r#####"
|
||||||
codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
|
codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
|
|
||||||
let mut buf = String::from(
|
|
||||||
"# Assists\n\nCursor position or selection is signified by `┃` character.\n\n",
|
|
||||||
);
|
|
||||||
|
|
||||||
for assist in assists {
|
|
||||||
let before = assist.before.replace("<|>", "┃"); // Unicode pseudo-graphics bar
|
|
||||||
let after = assist.after.replace("<|>", "┃");
|
|
||||||
let docs = format!(
|
|
||||||
"
|
|
||||||
## `{}`
|
|
||||||
|
|
||||||
{}
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// BEFORE
|
|
||||||
{}
|
|
||||||
// AFTER
|
|
||||||
{}```
|
|
||||||
",
|
|
||||||
assist.id,
|
|
||||||
assist.doc,
|
|
||||||
hide_hash_comments(&before),
|
|
||||||
hide_hash_comments(&after)
|
|
||||||
);
|
|
||||||
buf.push_str(&docs);
|
|
||||||
}
|
|
||||||
|
|
||||||
codegen::update(&project_root().join(codegen::ASSISTS_DOCS), &buf, mode)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hide_hash_comments(text: &str) -> String {
|
fn hide_hash_comments(text: &str) -> String {
|
||||||
text.split('\n') // want final newline
|
text.split('\n') // want final newline
|
||||||
.filter(|&it| !(it.starts_with("# ") || it == "#"))
|
.filter(|&it| !(it.starts_with("# ") || it == "#"))
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use std::{fmt, fs, path::PathBuf};
|
use std::{fmt, fs, path::PathBuf};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{self, extract_comment_blocks_with_empty_lines, Mode},
|
codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode},
|
||||||
project_root, rust_files, Result,
|
project_root, rust_files, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ pub fn generate_feature_docs(mode: Mode) -> Result<()> {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Feature {
|
struct Feature {
|
||||||
id: String,
|
id: String,
|
||||||
path: PathBuf,
|
location: Location,
|
||||||
doc: String,
|
doc: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ impl Feature {
|
||||||
let id = block.id;
|
let id = block.id;
|
||||||
assert!(is_valid_feature_name(&id), "invalid feature name: {:?}", id);
|
assert!(is_valid_feature_name(&id), "invalid feature name: {:?}", id);
|
||||||
let doc = block.contents.join("\n");
|
let doc = block.contents.join("\n");
|
||||||
acc.push(Feature { id, path: path.clone(), doc })
|
acc.push(Feature { id, location: Location::new(path.clone()), doc })
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -69,20 +69,6 @@ fn is_valid_feature_name(feature: &str) -> bool {
|
||||||
|
|
||||||
impl fmt::Display for Feature {
|
impl fmt::Display for Feature {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
writeln!(f, "=== {}", self.id)?;
|
writeln!(f, "=== {}\n**Source:** {}\n{}", self.id, self.location, self.doc)
|
||||||
let path = self.path.strip_prefix(&project_root()).unwrap().display().to_string();
|
|
||||||
let path = path.replace('\\', "/");
|
|
||||||
let name = self.path.file_name().unwrap();
|
|
||||||
|
|
||||||
//FIXME: generate line number as well
|
|
||||||
writeln!(
|
|
||||||
f,
|
|
||||||
"**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/{}[{}]",
|
|
||||||
path,
|
|
||||||
name.to_str().unwrap(),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
writeln!(f, "{}", self.doc)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -191,7 +191,7 @@ Release: release:{}[]
|
||||||
let path = changelog_dir.join(format!("{}-changelog-{}.adoc", today, changelog_n));
|
let path = changelog_dir.join(format!("{}-changelog-{}.adoc", today, changelog_n));
|
||||||
fs2::write(&path, &contents)?;
|
fs2::write(&path, &contents)?;
|
||||||
|
|
||||||
for &adoc in ["manual.adoc", "generated_features.adoc"].iter() {
|
for &adoc in ["manual.adoc", "generated_features.adoc", "generated_assists.adoc"].iter() {
|
||||||
let src = project_root().join("./docs/user/").join(adoc);
|
let src = project_root().join("./docs/user/").join(adoc);
|
||||||
let dst = website_root.join(adoc);
|
let dst = website_root.join(adoc);
|
||||||
fs2::copy(src, dst)?;
|
fs2::copy(src, dst)?;
|
||||||
|
|
Loading…
Reference in a new issue