如何将命名导入转换为VSCODE中的默认导入?

发布于 2025-02-03 14:22:55 字数 1588 浏览 1 评论 0 原文

我正在使用VSCODE,并试图纠正我项目中多个文件中的导入方式以更具性能的格式 - VSCODE是否具有有助于此功能的功能?可以通过内置查找和替换来完成吗?还是其他一些VSCODE功能可以执行此操作?

我看起来像这样的导入( senterance-ux 是真实模块名称的混淆版本,因为我不想要模块的特定答案):

import { Foo, Bar as BarBar } from '@substance-ux/glyphs';

或也许:

import {
  GlyphWithLongName as LongName,
  GlyphWithExtraLongName as ExtraLong
} from '@substance-ux/glyphs';

和i 需要将其转换为此样式,与我们项目其他位置的导入匹配:(

import Foo from '@substance-ux/glyphs/Foo';
import BarBar from '@substance-ux/glyphs/Bar';

作为

import LongName from '@substance-ux/glyphs/GlyphWithLongName';
import ExtraLong from '@substance-ux/glyphs/GlyphWithExtraLongName';

一边,'@sentance-ux/glyphs/glyphwithwithextralongname'之类的文件已经存在,并且包的文档说@senterance-ux/glyphs 模块在导入上运行很多代码,这会减慢开发的构建。)

现在,如果我知道我有一种格式或另一种格式,或者我知道多少我可以依靠查找和替换,例如,我可以使用一些正则(查找: import \ {(。*),(。*),(。 glyphs)'; 替换从'$ 3/$ 1'; \ nimport $ 1从'$ 3/$ 2'; )和在VSCODE中查找并替换功能

但是,如果我有一口气的导入数量或混合样式(有些“有些”),如果我尝试一口气做到这一点,我就会完全取消。

我知道 snippets 文本语法,但我认为他们无法处理可变数量的捕获组?还是可以?

在没有扩展的VSCODE中,这是否可以吗?

I'm using VSCode and trying to correct the way imports are written across multiple files in my project to a more performant format - does VSCode have functionality that would facilitate this? Could it be done with the built-in find and replace? Or is some other VSCode feature able to do this?

The imports I've got look like this (substance-ux is an obfuscated version of a real module name, as I don't want module specific answers):

import { Foo, Bar as BarBar } from '@substance-ux/glyphs';

Or maybe:

import {
  GlyphWithLongName as LongName,
  GlyphWithExtraLongName as ExtraLong
} from '@substance-ux/glyphs';

And I need to convert it into this style, matching imports elsewhere in our project:

import Foo from '@substance-ux/glyphs/Foo';
import BarBar from '@substance-ux/glyphs/Bar';

Or this:

import LongName from '@substance-ux/glyphs/GlyphWithLongName';
import ExtraLong from '@substance-ux/glyphs/GlyphWithExtraLongName';

(As an aside, the files like '@substance-ux/glyphs/GlyphWithExtraLongName' already exist and the docs for the package say that the @substance-ux/glyphs module runs a lot of code on import, which slows down development builds.)

Now if I know I have one format or the other, or I know how many then I'm ok to rely on find and replace, e.g. I can use a bit of regex (find: import \{ (.*), (.*) } from '(@substance-ux/glyphs)'; replace import $1 from '$3/$1';\nimport $2 from '$3/$2';) and the Find and Replace feature in VSCode.

But if I have variable number of imports, or mixed style (some 'as' some not) I become completely unstuck, if I try to do this in one go.

I know snippets can do regex capture and some clever replacing using TextMate syntax, but I don't think they can handle variable number of capture groups? Or can they?

Is this possible in VSCode without extensions etc?

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

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

发布评论

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

评论(1

疑心病 2025-02-10 14:22:55

摘要可以处理可变数量的参数 - 我回答了许多问题 - 但我认为它们不适用于您的格式,而参数已经存在,而不是作为摘要的一部分输入。我真的不认为您的情况有非扩展或非记录解决方案。

使用我写的扩展名,,您可以在钥匙扣或设置中编写JavaScript。

此键键 - 在您的 keybindings.json 中 - 将执行您想要的操作(也可以是设置,以便命令出现在命令调色板中):

{
  "key": "alt+f",                 // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {

    "find": "(import\\s*{\\s*)([^}]+?)(\\s*}\\s*from\\s*')([^']+)';",
    
    "replace": [
      "${",
      
        "const from = `$4`;",   // capture group 4 from the find
        
        // using capture group 2 from the find regex
        // note backticks around $2 below because it could contain newlines

        "const splitImports = [...`$2`.matchAll(/(\\w+)(?:$|,)|(\\w+)\\s?as\\s?(\\w+)/g)];",
        
        "let str = '';",
        
        "splitImports.forEach((match, index) => { ",

          // don't add a newline if last match
          "let newline = '';",
          "if (index < splitImports.length - 1) newline = '\\n';",
          
          // note double-escapes necessary
          "if (match[1]) str += `import ${match[1]} from \\'${from}/${match[1]}\\';${newline}`;",
          "if (match[2]) str += `import ${match[3]} from \\'${from}/${match[2]}\\';${newline}`;",
        "}); ",
        
        "return str;",
      
      "}$"
    ],
    
    "isRegex": true,
    // "restrictFind": "line",
  },
  // "when": "editorLangId == javascript"   // if you want it
}

这将在整个文件上工作,作为演示显示。如果您想测试它或只是让它执行您选择的行,请启用“ drimentfind”:“ line”

Snippets can handle a variable number of arguments - I have answered a number of SO questions showing that - but I don't think they would work with your format where the arguments are already there rather than to be entered as part of the snippet. I really don't think there is a non-extension or non-script solution to your situation.

Using an extension that I wrote, Find and Transform, you can write javascript in a keybinding or a setting.

This keybinding - in your keybindings.json - will do what you want (it could also be a setting so that the command would appear in the Command Palette):

{
  "key": "alt+f",                 // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {

    "find": "(import\\s*{\\s*)([^}]+?)(\\s*}\\s*from\\s*')([^']+)';",
    
    "replace": [
      "${",
      
        "const from = `$4`;",   // capture group 4 from the find
        
        // using capture group 2 from the find regex
        // note backticks around $2 below because it could contain newlines

        "const splitImports = [...`$2`.matchAll(/(\\w+)(?:$|,)|(\\w+)\\s?as\\s?(\\w+)/g)];",
        
        "let str = '';",
        
        "splitImports.forEach((match, index) => { ",

          // don't add a newline if last match
          "let newline = '';",
          "if (index < splitImports.length - 1) newline = '\\n';",
          
          // note double-escapes necessary
          "if (match[1]) str += `import ${match[1]} from \\'${from}/${match[1]}\\';${newline}`;",
          "if (match[2]) str += `import ${match[3]} from \\'${from}/${match[2]}\\';${newline}`;",
        "}); ",
        
        "return str;",
      
      "}$"
    ],
    
    "isRegex": true,
    // "restrictFind": "line",
  },
  // "when": "editorLangId == javascript"   // if you want it
}

This would work on the entire file as the demo shows. If you wanted to test it or just have it do the lines you select, enable "restrictFind": "line".

modify imports to a different pattern

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