Skip to content

Syntax highlighting for code in comments #209

@plevold

Description

@plevold

Is your feature request related to a problem? Please describe.
Note: I'm not sure whether fortls or the Modern Fortran VSCode extension is the right place for this suggestion. Feel free to let me know if it should be moved. Also, it is somewhat related to #208, but I think it belongs in a separate issue.

The Rust VSCode extension / Rust-analyzer has some interesting features when it comes to comments which I think could fit nicely into Modren Fortran / fortls as well. Take the following example from the PyO3 crate:

/// A container type for (mutably) accessing [`PyClass`] values
///
/// `PyCell` autodereferences to [`PyAny`], so you can call `PyAny`'s methods on a `PyCell<T>`.
///
/// # Examples
///
/// This example demonstrates getting a mutable reference of the contained `PyClass`.
/// ```rust
/// use pyo3::prelude::*;
///
/// #[pyclass]
/// struct Number {
///     inner: u32,
/// }
///
/// #[pymethods]
/// impl Number {
///     fn increment(&mut self) {
///         self.inner += 1;
///     }
/// }
///
/// # fn main() -> PyResult<()> {
/// Python::with_gil(|py| {
///     let n = PyCell::new(py, Number { inner: 0 })?;
///
///     let n_mutable: &mut Number = &mut n.borrow_mut();
///     n_mutable.increment();
///
///     Ok(())
/// })
/// # }
/// ```
/// For more information on how, when and why (not) to use `PyCell` please see the
/// [module-level documentation](self).
#[repr(C)]
pub struct PyCell<T: PyClassImpl> {
    ob_base: <T::BaseType as PyClassBaseType>::LayoutAsBase,
    contents: PyCellContents<T>,
}

In VSCode, this renders as follows:
image

Hovering the struct (type) gives this:
image

Describe the solution you'd like
There's two nice things with the Rust-analyzer way which I think could be considered for fortls as well:

  • The code example in the documentation comment is identified and highlighted with syntax highlighting like the regular code.
  • The hover popup renders the documentation comment with syntax highlighting and rich text formatting. I think this is covered by Change hover to use MarkupContent #45 if I'm not mistaken.

Additional context
This is probably not a trivial task even if it's technically reasonable to implement. The Fortran community at large should probably agree on a way of writing documentation comments which fortls then can pick up. Rust seems to use markdown syntax for this. Another option is to use Doxygen commands like \code. Personally I like the markdown approach, but there are many considerations to be made here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestlsp/hoverIssues related with hovering requests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions