AI Continues to Impress

January 22nd, 2024

Recently I have been making a point to use Zed's built-in AI assistant during my day-to-day coding. It's no secret that I'm a bit of an AI skeptic, but I want to keep an open mind and give AI the chance to impress me.

What follows are some accounts of recent instances where Zed's AI integration has left me feeling impressed.

Writing docs with AI

In the past two weeks the team has been hosting what we've affectionately termed "docs parties" where we all hang out for an hour or two and document the various bits of Zed's codebase.

During one of these parties I found myself needing to document two structs with the same fields: an internal settings struct and a separate struct for serialization. One of these structs had its fields documented while the other did not:

#[derive(Debug, Clone, Deserialize)]
pub struct LanguageSettings {
    pub tab_size: NonZeroU32,
    pub hard_tabs: bool,
    pub soft_wrap: SoftWrap,
    // ...
}
 
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct LanguageSettingsContent {
    /// How many columns a tab should occupy.
    ///
    /// Default: 4
    #[serde(default)]
    pub tab_size: Option<NonZeroU32>,
    /// Whether to indent lines using tab characters, as opposed to multiple
    /// spaces.
    ///
    /// Default: false
    #[serde(default)]
    pub hard_tabs: Option<bool>,
    /// How to soft-wrap long lines of text.
    ///
    /// Default: none
    #[serde(default)]
    pub soft_wrap: Option<SoftWrap>,
    // ...
}

While I could have copied the doc comments over by hand, this struct had 16 fields and the inbalance of lines in each doc comment would have made a multi-cursor maneuver a bit tricky.

I decided to enter this prompt into the inline assistant to see what would happen:

Copy the doc comments from the fields in LanguageSettingsContent and add them to the fields in LanguageSettings. Ignore the note about the default value. Just bring over the doc comments.

The AI copied over the doc comments verbatim—sans the note about the default value that I asked it to ignore—and put them on the corresponding fields:

#[derive(Debug, Clone, Deserialize)]
pub struct LanguageSettings {
    /// How many columns a tab should occupy.
    pub tab_size: NonZeroU32,
    /// Whether to indent lines using tab characters, as opposed to multiple
    /// spaces.
    pub hard_tabs: bool,
    /// How to soft-wrap long lines of text.
    pub soft_wrap: SoftWrap,
    // ...

Translating code with AI

In my spare time I've been rebuilding the bones of my personal website. As part of this, I need to translate my templates from Tera over to a new templating system I'm building on top of auk.

I needed to port this small component I had written over to Auk's DSL:

<div class="pv2 flex items-end">
    <div class="pv1 w-20"> {{ label }} </div>
    <div class="pv1 w-80">
        <progress
            class="language-bar flex"
            value="{{ value }}"
            max="10"
        ></progress>
    </div>
</div>

Once again, I thought it would be interesting to see what the Zed assistant could do with this. I prompted it with:

Rewrite this HTML using auk's DSL like the post_link function above.

The end result that the assistant spit out looked identical to what I would have produced by hand:

pub fn language_stat(label: &str, value: u32) -> HtmlElement {
    div()
        .class("pv2 flex items-end")
        .child(div().class("pv1 w-20").text_content(label))
        .child(
            div().class("pv1 w-80").child(
                progress()
                    .class("flex language-bar")
                    .value(value.to_string())
                    .max("10"),
            ),
        )
}

I was especially struck by the ease at which the assistant was able to adapt to this brand-new DSL that I had created. By simply composing in the same buffer as a few other examples, the AI was able to piece together enough of the context to correctly form.

Since discovering that the Zed assistant is quite adept at converting between Tera and Auk, I've continued using it over the course of the migration to cut down on the manual grunt work. At time of writing there are almost 1,300 lines of Auk template code, the majority of which were written with the help of the AI assistant:

λ nix-shell -p cloc --command "cloc --vcs=git crates/site/src/{components,templates}"
 
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Rust                            16            130              2           1293
-------------------------------------------------------------------------------
SUM:                            16            130              2           1293
-------------------------------------------------------------------------------

While I still don't buy into the notion that AI will make software developers obsolete, these experiences have convinced me there is definitely room for a judicious integration of AI into our development tools and workflows.

At Zed we're exploring what it would look like to harness the power of AI to empower developers as they write software, and we're just getting started.


This post originally appeared on maxdeviant.com.