9

我正在寻找一种类似于 C(++)#if 0的方式来注释掉整个 Scala 源代码,以便在一段时间内保留实验性或过期代码。

我尝试了几种替代方法,想听听您使用的是什么,以及您是否想出了更好的方法?

// Simply block-marking N lines by '//' is one way... 
//  <tags> """ anything

我的编辑器使这很容易,但这并不是真正的事情。它很容易与实际的单行注释混合。

然后我认为有原生 XML 支持,所以:

<!-- 
  ... did not work
-->

包装在 XML 中是有效的,除非你<tags>在块中:

class none { val a= <ignore>
  ...
  cannot have //<tags> <here> (not even in end-of-line comments!)
</ignore> }

对于多行字符串来说,这似乎是最好的,但是有很多样板文件(在 Scala 中并不流行)来取悦编译器(如果你在一个类或一个对象中这样做,那就更少了):

object none { val ignore= """ This seems like
  ...
  <truly> <anything goes> but three "'s of course
""" }

这样做的“正确”方法可能是:

/***
  /*
  ... works but not properly syntax highlighed in SubEthaEdit (or StackOverflow)
  */
***/

/*..但这与and only匹配,而*/不是 ie /***to ***/。这意味着需要平衡块内的注释。并且 - SubEthaEdit 当前的 Scala 语法高亮模式在这方面失败了。

作为比较,Lua--[==[匹配]==]等等。我觉得我被宠坏了?

那么 - 我正在监督一些有用的技巧吗?

4

5 回答 5

5

为什么不直接使用您的源代码控制机制?将代码分开,将其作为单独的文件签入并忘记它。我不希望我的日常代码库被这种东西弄得一团糟。

但是请注意,如果您不经常使用此代码(例如在自动化测试中等),它将遭受代码腐烂。一旦您注释掉或以其他方式搁置这些东西,依赖关系就会继续,您会发现不久之后它就不会链接到现有的代码库。

于 2012-11-29T17:12:56.940 回答
4

我修改了 Scala 模式SyntaxDefinition.xml以支持/***...***/样式注释。

这与 Scala 解析器对嵌套/*...*/注释的支持不同,但我没有为我的编辑器找到表达这一点的方法。

如果有人想做同样的事情,这里是:

<!-- AK 30-Nov-2012
-
- The Scala parser handles nested '/*...*/' style comments, but the SEE
- syntax highlighting seems not. 
-
- We introduce '/***...***/' style comments (starting with three asterisks
- since JavaDoc uses '/**..*/' style) and deeper levels, to be used for
- blocking out code blocks, even if they contain '/*..*/' comments within.
-
- Note: Original comment handling misses a 'type="comment"' field. Is that vital?
-
- Test: If this works right, the following will be highlighted as a single comment:
-     <<
-       /***
-       */
-       ***/    <- green, not black (note: Scala parses these differently; this is just to test the mode)
-     <<
-->
<state id="Multilevel Comment AK" color="#236E25" type="comment" font-style="italic">
  <begin><regex>/\*\*(?'commentCatch'\*+)</regex></begin>
  <end><regex>(?#see-insert-start-group:commentCatch)\*\*/</regex></end>
  <import mode="Base" state="EmailAndURLContainerState" keywords-only="yes"/>
</state>

您可能还想添加type="comment"到现有的少数评论突出显示规则。我不确定这是否至关重要(除了 Scala 的其他模式)。

关于SubEthaEdit 模式的信息。

于 2012-11-30T11:02:46.413 回答
4

您还漏掉了一个选项。任何类型的注释都有禁用语法突出显示的缺点,并且不包含在 IDE 重构(Emacs+Ensime、IDEA、Eclipse 等)或其他代码智能工具中,因此我更喜欢以下方法:

def ignore(block: => Any) = ()
def ignoreIf(cond: Boolean)(block: => Any): Unit = if (!cond) block

ignore {
  // experimental and/or disabled code
  syntaxHighlightingEnabled(true, 3, "foobar")
}

ignoreIf(SomeFeatureEnabled) {
  // experimental and conditionally enabled code
  syntaxHighlightingEnabled(true, 3, "foobar")
}
于 2014-09-10T11:42:55.913 回答
0

我使用“删除度”。(1) 注释掉。(2) 如果它是我不再需要但可能在以后/其他地方发现有用的代码,我有一个“.boneyard”文件夹,我将代码片段放在其中(就在生产源代码树之外)——非常方便。(3) 只需删除它,如果我需要它,请依赖源代码管理。

于 2015-07-26T15:36:19.733 回答
0

受我使用的 Lua 编程语言的启发:

def */ = () // noop function, def or val


/*

println("Hello world!")
// more code

*/

要启用整个代码块,只需在“/*”中添加一个“/”,即

def */ = () // noop function


//*

println("Hello world!")
// more code

*/

现在它打印“Hello world!”。

于 2019-11-06T14:48:51.400 回答