【问题标题】:Auto-format on method save in Pharo在 Pharo 中保存方法时自动格式化
【发布时间】:2011-08-17 08:57:31
【问题描述】:

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

【问题讨论】:

    标签: coding-style ide pharo


    【解决方案1】:

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

    【讨论】:

    • 如果您发现设置“接受时自动格式化”为时已晚,并且如果想要自动格式化一个完整的包,您也可以这样做。选择包的实体(例如类或方法),然后单击上下文菜单中的“重构范围 > 包”。在新浏览器中选择“重构 > 重新格式化”。
    • 在 Pharo v3 上,设置浏览器发生了变化,您提到的项目似乎不再存在。它说格式化程序是RBConfigurableFormatter,但是当我保存方法时不会发生自动格式化。我也无法在设置浏览器中找到打开/关闭它的选项。
    【解决方案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.**
    

    【讨论】:

      【解决方案3】:

      对于 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
      

      【讨论】:

        【解决方案4】:

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

        我已经为此发布了PharoPackageFormatter

        下面是一个你将如何使用它的例子:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-30
          • 2010-12-13
          • 1970-01-01
          • 2020-09-01
          • 1970-01-01
          • 2019-02-07
          • 2018-08-05
          • 2012-06-13
          相关资源
          最近更新 更多