Textmate 片段在评论中添加文件名和路径?

发布于 2024-09-09 03:14:13 字数 1131 浏览 8 评论 0原文

我使用 Textmate 中的现有代码片段来减少创建控制器和模型的重复次数。该代码片段效果很好,但我想在每个文件的末尾添加注释。例如:

/* End of file filename.php */  
/* Location: ./system/application/controllers/filename.php */

第一行很简单:

/* End of file ${TM_FILENAME} */

第二部分与 TM_FILEPATH 变量几乎一样简单:

/* Location: ./${TM_FILEPATH} */

问题是,我不希望返回整个文件路径,只返回“系统”(如果存在)或“应用程序”之后的任何内容如果不。例如,使用 TM_FILEPATH 返回:

/* Location: ./path/from/root/system/application/controllers/filename.php */
-or-
/* Location: ./path/from/root/application/controllers/filename.php */

...当我想要时:

/* Location: ./system/application/controllers/filename.php */
-or-
/* Location: ./application/controllers/filename.php */

我认为这将是一些正则表达式技巧,但我不知道如何实现。请问有什么建议吗?


更新:我刚刚找到了 TextMate 变量 TM_PROJECT_DIRECTORY,其中包含我想要从 TM_FILEPATH 中删除的信息(如果这会让事情变得更容易的话)。

因此,TM_FILEPATH 产生以下内容:

/path/from/root/system/application/controllers/filename.php

TM_PROJECT_DIRECTORY 产生以下内容:

/path/from/root

I'm using an existing snippet in Textmate to reduce the repetition of creating controllers and models. The snippet works great, but I'd love to add a comment to the end of each file. For example:

/* End of file filename.php */  
/* Location: ./system/application/controllers/filename.php */

The first line is easy:

/* End of file ${TM_FILENAME} */

The second part is almost as easy with the TM_FILEPATH variable:

/* Location: ./${TM_FILEPATH} */

The problem is, I don't want the entire file path returned, just anything AFTER 'system' if it exists, or 'application' if not. For instance, using TM_FILEPATH returns this:

/* Location: ./path/from/root/system/application/controllers/filename.php */
-or-
/* Location: ./path/from/root/application/controllers/filename.php */

...when I want:

/* Location: ./system/application/controllers/filename.php */
-or-
/* Location: ./application/controllers/filename.php */

I assume it is going to be some regex trickery, but I have no idea how. Any suggestions please?


UPDATE: I just found the TextMate variable TM_PROJECT_DIRECTORY which contains the info that I want REMOVED from TM_FILEPATH if that makes things any easier.

So, whereas TM_FILEPATH produces this:

/path/from/root/system/application/controllers/filename.php

TM_PROJECT_DIRECTORY produces this:

/path/from/root

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

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

发布评论

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

评论(3

笑咖 2024-09-16 03:14:13

我不了解 Textmate,但是你可以使用 CI 常量 FCPATH 吗?这是文件的完整服务器路径。

I don't know about Textmate, but could you use the CI constant FCPATH? This is the full server path to the file.

花落人断肠 2024-09-16 03:14:13

我对控制器和模型使用两个不同的片段(因为语法相似,但有点不同;IE:每个控制器都需要一个索引函数,但模型不需要)。我只是按如下方式对它们进行硬编码...

控制器:

/* Location: ./application/controllers/${TM_FILENAME} */

模型:

/* Location: ./application/models/${TM_FILENAME} */

由于我总是将应用程序文件夹从系统文件夹中拉出,并将系统文件夹放在不同的目录中,所以效果很好。我还在应用程序上添加了一个制表符,以防万一我重命名了应用程序目录。

@TM_PROJECT_DIRECTORY:我认为只有当您将文件作为项目打开时,这才有效。我使用的是基于 Textmate 的电子文本编辑器,所以它可能有点不同。

希望这有帮助。

I use two different snippets for controllers and models (since the syntax is similar, but just a bit different; I.E.: every controller needs an index function, but models don't). I just hard code them as follows...

Controller:

/* Location: ./application/controllers/${TM_FILENAME} */

Model:

/* Location: ./application/models/${TM_FILENAME} */

Since I always pull the application folder out of the system folder, and put the system folder in a different directory, this works out great. I also added a tabstop on application, just in case I renamed the application directory.

@TM_PROJECT_DIRECTORY: I think this only works if you open your files as a project. I use e text editor, which is based on Textmate, so it might be a little bit different.

Hope this helps.

死开点丶别碍眼 2024-09-16 03:14:13

这是我想出的解决方案。我不知道这是否是最好的方法,但它似乎有效:

/* End of file ${TM_FILENAME} */
/* Location: ${TM_FILEPATH/(.*?)(\/system)?(\/application.*)/(?1:).$2$3/} */

我会根据我的理解将其分解;)

${TM_FILEPATH      - The 'source' string TextMate variable
/                  - Indicates next chars are 'pattern'
(.*?)              - Group 1: Zero or more of any character. ? Makes it non-greedy
(\/system)?        - Group 2: /system but it's optional because of the ?
(\/application.*)  - Group 3: /application and any other characters
/                  - Indicates next chars are 'replacement'
(?1:).$2$3         - If Group 1 is found, replace with blank, then a dot, Group 2, Group 3.
/                  - Indicates regex is finished.
}                  - Closes off TextMate variable.

Here is the solution I came up with. I have no idea if it is the best way, but it seams to work:

/* End of file ${TM_FILENAME} */
/* Location: ${TM_FILEPATH/(.*?)(\/system)?(\/application.*)/(?1:).$2$3/} */

I'll break it down as much as I understand it ;)

${TM_FILEPATH      - The 'source' string TextMate variable
/                  - Indicates next chars are 'pattern'
(.*?)              - Group 1: Zero or more of any character. ? Makes it non-greedy
(\/system)?        - Group 2: /system but it's optional because of the ?
(\/application.*)  - Group 3: /application and any other characters
/                  - Indicates next chars are 'replacement'
(?1:).$2$3         - If Group 1 is found, replace with blank, then a dot, Group 2, Group 3.
/                  - Indicates regex is finished.
}                  - Closes off TextMate variable.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文