vim 中自动缩进 javascript,从单行代码到完全缩进代码

发布于 2025-01-16 17:12:30 字数 1926 浏览 4 评论 0原文

我正在使用 vim 进行编码,即使用 javascript 进行编码。我希望能够自动缩进 javascript 代码。我的主要用例是能够自动从单行 javascript 代码跳转到完全缩进的漂亮打印代码(就像 https ://beautifier.io/ 就可以了)。

尽管在谷歌和SO上寻求帮助,但在缩进代码是一行的特定情况下,我无法找到任何与自动缩进相关的问题。

示例输入文件:

import { BASE_URL } from "@/constants/config"; import { request } from "@/utils/request"; export const FOOTER_ENDPOINTS = { links: `${ BASE_URL} api/footer/links/`, } ; class Footer { async fetchLinks() { const { data } = await request({ url: FOOTER_ENDPOINTS.links, }); return data; } } export const FooterService = new Footer();

我期待以下输出:

import { BASE_URL } from "@/constants/config";
import { request } from "@/utils/request";

export const FOOTER_ENDPOINTS = {
  links: `${BASE_URL}api/footer/links/`,
};

class Footer {
  async fetchLinks() {
    const { data } = await request({
      url: FOOTER_ENDPOINTS.links,
    });
    return data;
  }
}

export const FooterService = new Footer();

到目前为止,我未成功尝试 =Ggg=G 命令以及 :Autoformat 来自 vim-autoformat 插件和 pangloss/vim-javascript 插件。

我得到的最好提示是使用 maksimr/vim-jsbeautify 但结果并不像我预期的那样“打印得很漂亮”。也许我不知道的一些 editorconfig 属性可能有助于添加换行符并避免导入在不太长的时候被破坏?

import { 
    BASE_URL
} from "@/constants/config";
import {
    request
} from "@/utils/request";
export const FOOTER_ENDPOINTS = {
    links: `${BASE_URL}api/footer/links/`,
};
class Footer {
    async fetchLinks() {
        const {
            data
        } = await request({
            url: FOOTER_ENDPOINTS.links,
        });
        return data;
    }
}
export const FooterService = new Footer();

奖励:如果该解决方案还可以自动缩进类似于 dict 的实体(例如 javascript 或 json),我会感到惊讶。

I am using vim to code, namely in javascript. I would love to be able to auto-indent javascript code. My main use case is being able to automatically jump from a one-liner javascript code to a fully indented pretty-printed code (just as https://beautifier.io/ would do).

Despite looking for help on google and SO, I was not able to find any auto-indent related question in the specific case where code to indent is a one-liner.

Sample input file :

import { BASE_URL } from "@/constants/config"; import { request } from "@/utils/request"; export const FOOTER_ENDPOINTS = { links: `${ BASE_URL} api/footer/links/`, } ; class Footer { async fetchLinks() { const { data } = await request({ url: FOOTER_ENDPOINTS.links, }); return data; } } export const FooterService = new Footer();

I am expecting the following output :

import { BASE_URL } from "@/constants/config";
import { request } from "@/utils/request";

export const FOOTER_ENDPOINTS = {
  links: `${BASE_URL}api/footer/links/`,
};

class Footer {
  async fetchLinks() {
    const { data } = await request({
      url: FOOTER_ENDPOINTS.links,
    });
    return data;
  }
}

export const FooterService = new Footer();

So far i have unsuccessfully tried =G and gg=G commands, as well as :Autoformat from vim-autoformat plugin and pangloss/vim-javascript plugin.

Best hint I have is to use maksimr/vim-jsbeautify but the result is not as "pretty printed" as I would have expected. Maybe some editorconfig properties I do not know might help for adding line breaks and avoid imports being breaked when not too long ?

import { 
    BASE_URL
} from "@/constants/config";
import {
    request
} from "@/utils/request";
export const FOOTER_ENDPOINTS = {
    links: `${BASE_URL}api/footer/links/`,
};
class Footer {
    async fetchLinks() {
        const {
            data
        } = await request({
            url: FOOTER_ENDPOINTS.links,
        });
        return data;
    }
}
export const FooterService = new Footer();

BONUS : I would be amazing if the solution could also auto-indent dict-like entities such as javascript or json for instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惜醉颜 2025-01-23 17:12:30

作为通用文本编辑器,Vim 对于特定文件类型的功能是有限的。

开箱即用的 Vim 无法执行您所要求的操作,但它允许您使用外部工具来执行您所要求的操作。

你需要弄清楚的是:

  1. 使用什么外部工具,
  2. 如何配置它以获得期望的结果,
  3. 如何将其与 Vim 集成。

js-beautify

您提到的网络应用程序使用 js-beautify 因此,如果您对网络应用程序如何格式化代码感到满意,那么您应该尝试 js-beautify

这里的第一个问题是,网络应用程序实际上并没有按照您声称的方式格式化您的示例。它输出:

 import {
     BASE_URL
 } from "@/constants/config";
 import {
     request
 } from "@/utils/request";
 export const FOOTER_ENDPOINTS = {
     links: `${ BASE_URL} api/footer/links/`,
 };
 class Footer {
     async fetchLinks() {
         const {
             data
         } = await request({
             url: FOOTER_ENDPOINTS.links,
         });
         return data;
     }
 }
 export const FooterService = new Footer();

这恰好与本地 js-beautify 默认输出的内容相同。

如果该网络应用确实能够按照您想要的方式格式化您的代码,那么您必须尝试各种设置,直到满意为止,然后进入配置阶段。

但还有第二个问题:我只是花了几分钟摆弄设置,恐怕我必须指出这一点:

正如https://beautifier.io/ 所做的那样

显然是错误的。该网络应用程序无法实现您理想的格式,并且底层的 js-beautify 很可能受到类似的限制。

因此,您回到步骤#1:弄清楚要使用什么外部工具......这超出了本网站的范围。

Being the generalist text editor that it is, there is a limit to what Vim can do on its own with specific filetypes.

Out of the box, Vim is incapable of doing what you ask but it allows you to use external tools that do.

What you need to figure out is:

  1. what external tool to use,
  2. how to configure it to get the desired outcome,
  3. how to integrate it with Vim.

js-beautify

The web app you mention uses js-beautify so, if you are happy with how the web app formats your code, then you should give js-beautify a try.

The first problem, here, is that the web app doesn't actually format your sample the way you claim it does. It outputs this:

 import {
     BASE_URL
 } from "@/constants/config";
 import {
     request
 } from "@/utils/request";
 export const FOOTER_ENDPOINTS = {
     links: `${ BASE_URL} api/footer/links/`,
 };
 class Footer {
     async fetchLinks() {
         const {
             data
         } = await request({
             url: FOOTER_ENDPOINTS.links,
         });
         return data;
     }
 }
 export const FooterService = new Footer();

which happens to be identical to what local js-beautify outputs by default.

If that web app is really able to format your code the way you want, then you must try various settings until you are satisfied and then move on to the configuration phase.

But there is a second problem: I just spent a few minutes playing with the settings and I'm afraid I have to point out that this statement:

just as https://beautifier.io/ would do

is patently false. The web app can't do your ideal formatting and the underlying js-beautify is very likely to be similarly limited.

So you are back to step #1: figuring out what external tool to use… which is outside of the scope of this site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文