4

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

4

4 回答 4

3

当然,如果您使用 Refactoring Engine 和 OmniBrowser,有一个设置:在“设置浏览器”中导航到Refactoring Engine > Auto Format on Accept。还有Refactoring Engine > Auto Format on Display设置,它会在代码显示之前自动格式化代码。格式化设置本身位于Refactoring Engine > Configurable Formatter中。

于 2011-08-17T12:31:46.720 回答
2

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

NautilusUi -> compileSource: aText notifying: 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.**
于 2013-06-28T08:13:37.760 回答
0

对于 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
于 2016-04-22T23:48:02.417 回答
0

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

我已经为此发布了PharoPackageFormatter

这是一个如何使用它的示例:

packages := { 
    'AST-Core'.
    'AST-Core-Tests'
    }.
packages do: [ :each | PharoPackageFormatter formatPackageNamed: each ].
于 2021-09-04T23:37:02.380 回答