editor: Improve JSDoc extend comment on newline to follow convention (#30800)

Follow up for https://github.com/zed-industries/zed/pull/30768

This PR makes JSDoc auto comment on new line lot better by:

- Inserting delimiters regardless of whether previous delimiters have
trailing spaces or not
- When on start tag, auto-indenting both prefix and end tag upon new
line

This makes it correct as per convention out of the box. No need to
manually adjust spaces on every new line.


https://github.com/user-attachments/assets/81b8e05a-fe8a-4459-9e90-c8a3d70a51a2

Release Notes:

- Improved JSDoc auto-commenting on newline which now correctly indents
as per convention.
This commit is contained in:
Smit Barmase 2025-05-16 12:42:11 +05:30 committed by GitHub
parent cc3a28a8e8
commit 18d39e3f81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 264 additions and 139 deletions

View file

@ -755,12 +755,10 @@ pub struct LanguageConfig {
/// A list of preferred debuggers for this language.
#[serde(default)]
pub debuggers: IndexSet<SharedString>,
/// A character to add as a prefix when a new line is added to a documentation block.
/// Whether to treat documentation comment of this language differently by
/// auto adding prefix on new line, adjusting the indenting , etc.
#[serde(default)]
pub documentation_comment_prefix: Option<Arc<str>>,
/// Returns string documentation block of this language should start with.
#[serde(default)]
pub documentation_block: Option<Vec<Arc<str>>>,
pub documentation: Option<DocumentationConfig>,
}
#[derive(Clone, Debug, Serialize, Deserialize, Default, JsonSchema)]
@ -811,6 +809,19 @@ pub struct JsxTagAutoCloseConfig {
pub erroneous_close_tag_name_node_name: Option<String>,
}
/// The configuration for documentation block for this language.
#[derive(Clone, Deserialize, JsonSchema)]
pub struct DocumentationConfig {
/// A start tag of documentation block.
pub start: Arc<str>,
/// A end tag of documentation block.
pub end: Arc<str>,
/// A character to add as a prefix when a new line is added to a documentation block.
pub prefix: Arc<str>,
/// A indent to add for prefix and end line upon new line.
pub tab_size: NonZeroU32,
}
/// Represents a language for the given range. Some languages (e.g. HTML)
/// interleave several languages together, thus a single buffer might actually contain
/// several nested scopes.
@ -889,8 +900,7 @@ impl Default for LanguageConfig {
completion_query_characters: Default::default(),
debuggers: Default::default(),
significant_indentation: Default::default(),
documentation_comment_prefix: None,
documentation_block: None,
documentation: None,
}
}
}
@ -1810,21 +1820,12 @@ impl LanguageScope {
.unwrap_or(false)
}
/// A character to add as a prefix when a new line is added to a documentation block.
/// Returns config to documentation block for this language.
///
/// Used for documentation styles that require a leading character on each line,
/// such as the asterisk in JSDoc, Javadoc, etc.
pub fn documentation_comment_prefix(&self) -> Option<&Arc<str>> {
self.language.config.documentation_comment_prefix.as_ref()
}
/// Returns prefix and suffix for documentation block of this language.
pub fn documentation_block(&self) -> &[Arc<str>] {
self.language
.config
.documentation_block
.as_ref()
.map_or([].as_slice(), |e| e.as_slice())
pub fn documentation(&self) -> Option<&DocumentationConfig> {
self.language.config.documentation.as_ref()
}
/// Returns a list of bracket pairs for a given language with an additional