在 Pharo 中保存方法时自动格式化

发布于 2024-11-30 03:44:17 字数 68 浏览 2 评论 0原文

Pharo 有一个内置的代码格式化程序。我希望每当我保存方法时,Pharo 都会忽略我的所有格式并自动格式化代码。可以吗?

Pharo has a built-in code formatter. I want that whenever I save a method, Pharo ignores all my formatting and auto-formats the code instead. Can that be done?

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

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

发布评论

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

评论(4

半衾梦 2024-12-07 03:44:17

当然,如果您使用重构引擎和 OmniBrowser,则有一个设置:在“设置浏览器”中导航至“重构引擎”>“重构引擎”。接受时自动格式化。还有设置重构引擎>显示时自动格式化,在显示之前自动格式化代码。格式化设置本身位于重构引擎>中可配置的格式化程序

Sure, there is a setting if you use the Refactoring Engine and OmniBrowser: In the "Settings Browser" navigate to Refactoring Engine > Auto Format on Accept. There is also the setting Refactoring Engine > Auto Format on Display, which automatically formats the code before it is displayed. The formatting settings themselves are in Refactoring Engine > Configurable Formatter.

一曲琵琶半遮面シ 2024-12-07 03:44:17

我想出了一种技巧,来处理自格式化方法保存。它可能会派上用场。

在 NautilusUi -> 中compileSource: aText notification: aController

在开头添加这一行。

self refactor formatSourceCode.

也就是说,这将在保存功能上创建自动格式。我也承认,这不是正确的方法,但它对我有用。

 **compileSource: aText notifying: aController
        | source category method |
        self refactor formatSourceCode.
        source := aText asString.
        category := self selectedCategory.
        method := self selectedMethod.
        category ifNil: [ method ifNotNil: [ category := method protocol. ]. ].
        (category isNil and: [ method isNil. ])
            ifTrue: [

                source first isUppercase
                    ifTrue: [ ^ self compileAClassFrom: source notifying: aController. ].
                category := Categorizer default.
                ]
            ifFalse: [

                (category = self allLabel and: [ self selectedMethod notNil. ])
                    ifTrue: [ category := self selectedMethod protocol. ].
                ].
        self compileAMethodFromCategory: category withSource: source notifying: aController.**

I figured out a sort of hack , to deal with self formating method saves. It might come in handy.

In NautilusUi -> compileSource: aText notifying: aController

add this line in the begining.

self refactor formatSourceCode.

That is this will create the auto format on save functionality. Also i admit, that this is not the right way to do this, but it works for me.

 **compileSource: aText notifying: aController
        | source category method |
        self refactor formatSourceCode.
        source := aText asString.
        category := self selectedCategory.
        method := self selectedMethod.
        category ifNil: [ method ifNotNil: [ category := method protocol. ]. ].
        (category isNil and: [ method isNil. ])
            ifTrue: [

                source first isUppercase
                    ifTrue: [ ^ self compileAClassFrom: source notifying: aController. ].
                category := Categorizer default.
                ]
            ifFalse: [

                (category = self allLabel and: [ self selectedMethod notNil. ])
                    ifTrue: [ category := self selectedMethod protocol. ].
                ].
        self compileAMethodFromCategory: category withSource: source notifying: aController.**
梦晓ヶ微光ヅ倾城 2024-12-07 03:44:17

对于 OmniBrowser,我添加了这个新方法:

OBTextMorphEditorWithShout>>accept
"
This method did not previously exist for this class.
It is a subclass hook to auto format code on accept.
"

(ORCmdFormat on: 'TargetIsNotUsed' for: self model) execute.
super accept

For OmniBrowser, I added this new method:

OBTextMorphEditorWithShout>>accept
"
This method did not previously exist for this class.
It is a subclass hook to auto format code on accept.
"

(ORCmdFormat on: 'TargetIsNotUsed' for: self model) execute.
super accept
若水微香 2024-12-07 03:44:17

不完全是OP所要求的,但我认为这是值得考虑的。在项目中工作时,当 Pull 请求仅显示功能性代码更改而不会出现与编码风格相关的误报时,规范化代码格式将有助于同行评审者的工作。

我正是为此发布了 PharoPackageFormatter

以下是如何使用它的示例:

packages := { 
    'AST-Core'.
    'AST-Core-Tests'
    }.
packages do: [ :each | PharoPackageFormatter formatPackageNamed: each ].

Not exactly what OP is asking but I think this is worth considering. When working in a project, having code formatting normalized would facilitate the life of peer-reviewers when the Pull Requests show only functional code changes without the false positives related to coding style.

I've published PharoPackageFormatter precisely for this.

Here is an example of how you would use it:

packages := { 
    'AST-Core'.
    'AST-Core-Tests'
    }.
packages do: [ :each | PharoPackageFormatter formatPackageNamed: each ].
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文