XCode 4 注释格式(换行)

发布于 2024-12-03 18:54:10 字数 1493 浏览 2 评论 0原文

我认为多行注释理想情况下应如下所示:

/* this is a mult-line comment, which wraps lines at some reasonable length,
 * usually approximately 80 characters. this way, comments are easy to read
 * and, with any half way capable text editor, easy to edit without having
 * to manually reshuffle lines, line breaks, and the comment leader. */

// this is a mult-line comment, which wraps lines at some reasonable length,
// usually approximately 80 characters. this way, comments are easy to read
// and, with any half way capable text editor, easy to edit without having
// to manually reshuffle lines, line breaks, and the comment leader.

/* this is a mult-line comment, which wraps lines at some reasonable length,
   usually approximately 80 characters. this way, comments are easy to read
   and, with any half way capable text editor, easy to edit without having
   to manually reshuffle lines, line breaks, and the comment leader. */

但是,XCode 不支持管理这种风格的注释。您必须在正确的时间手动按回车键,将注释换行到适当的宽度,然后编辑它们就成为完整的 PITA。

或者,您只是从未按回车键并让编辑器将其包裹在编辑器屏幕的边缘。但如果您像我一样,您的编辑器比理想的自动换行行长度宽得多。

此外,XCode 还通过提供在编辑器中以 80 个字符呈现换行指南的功能来嘲笑我,但这纯粹是一个视觉功能,没有任何机制来支持它。这感觉就像把花园铲递给习惯使用反铲的人。

我是否需要在这里进行现实检查 - 我是否以错误的方式思考 - 或者 XCode 是否极其缺乏基本的段落样式注释格式?

经验丰富、负责任、专业的 Objective-C 开发人员在代码中进行大量注释时会做什么?帮助我看到这里的光。

注意:对于 XCode 3,我编写了一个手动脚本,该脚本重新格式化文本并将其绑定到热键。我还没有弄清楚如何在 XCode 4 中做到这一点。但如何编写 XCode 4 脚本与这个问题有点正交:必须用这样的基础知识来增强 IDE 的味道很糟糕,我的问题是关于 XCode 开发人员的风格和文化期望。

谢谢你的建议。

It's my opinion that multi-line comments should ideally appear something like this:

/* this is a mult-line comment, which wraps lines at some reasonable length,
 * usually approximately 80 characters. this way, comments are easy to read
 * and, with any half way capable text editor, easy to edit without having
 * to manually reshuffle lines, line breaks, and the comment leader. */

// this is a mult-line comment, which wraps lines at some reasonable length,
// usually approximately 80 characters. this way, comments are easy to read
// and, with any half way capable text editor, easy to edit without having
// to manually reshuffle lines, line breaks, and the comment leader.

/* this is a mult-line comment, which wraps lines at some reasonable length,
   usually approximately 80 characters. this way, comments are easy to read
   and, with any half way capable text editor, easy to edit without having
   to manually reshuffle lines, line breaks, and the comment leader. */

However, XCode has no support for managing comments in this style. You must manually hit return at the right time to wrap comments at a decent width, and then editing them becomes a complete PITA.

Or, you just never hit return and let the editor wrap it at the edge of your editor screen. But if you're like me, your editor is much wider than the ideal word wrapping line length.

Moreover, XCode taunts me by offering a feature to render a wrapping guide at 80 characters in the editor, yet this is purely a visual feature with no mechanics to back it up. This feels like handing a garden shovel to someone used to using a backhoe.

Do I need a reality check here - am I thinking the wrong way - or is XCode extremely lacking in basic paragraph-style comment formatting?

What do seasoned, responsible, professional Objective-C developers do when making substantial comments in their code? Help me see the light here.

Note: for XCode 3, I wrote a hand-rolled script which reformatted text and bound it to a hot key. I haven't figured out how to do this in XCode 4 yet. But how to script XCode 4 is a bit orthogonal to the issue: it smells bad to have to augment an IDE with such basics, my question is about the style and culture expectations of XCode developers.

Thanks for your advice.

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

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

发布评论

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

评论(4

小…红帽 2024-12-10 18:54:10

我同意它应该内置于 Xcode 中。但无论如何,以下是如何通过创建调用脚本的服务项并为其分配键盘快捷键来快速将其添加到 Xcode4:

  1. 打开 Automator。创建服务。
  2. 设置服务以让输出替换所选文本,并在您选择的 Xcode 副本中接收所选文本。
  3. 将“运行 Shell 脚本”操作拖入服务中作为唯一操作。
  4. 将默认 shell 脚本“cat”替换为“fmt -n -s -p -w80”。这会调用 fmt 实用程序来重新格式化文本,允许行以句点开头,折叠行内的空格,允许缩进段落,在 80 个字符的列处换行。检查 fmt 的手册页以了解其他选项。显然,你可以通过 emacs 或 markdown 或其他方式传输文本来发疯。
  5. 使用诸如“Reformat-to-80”之类的合理名称保存服务

现在,当您在 Xcode 中选择文本时,您将能够通过菜单栏选择服务将其重新格式化为 80 个字符:Xcode / Services / Reformat-到-80。

现在让我们为其指定一个键盘快捷键:

  1. 打开键盘首选项窗格,转到键盘快捷键选项卡。
  2. 在左侧窗格中选择“应用程序快捷方式”。
  3. 在右侧窗格中,使用添加按钮(+)为您之前选择的 Xcode 副本添加新的快捷方式。输入您用于该服务的确切名称并添加您选择的键盘快捷键。

现在,在 Xcode 中,您可以使用该键盘快捷键将所选文本替换为 shell 脚本的输出,这会将文本重新格式化为

I agree it should be built-in to Xcode. But anyway, here's how to add it to Xcode4 quickly by creating an service item that calls a script and assigning it a keyboard shortcut:

  1. Open Automator. Create a service.
  2. Set the service to let the output replace the selected text, and to receive selected text in the copy of Xcode you choose.
  3. Drag in the "Run Shell Script" action as the only action in your service.
  4. Replace the default shell script "cat" with "fmt -n -s -p -w80". This calls the fmt utility to reformat your text, allowing lines to begin with a period, collapsing whitespace within lines, allowing indented paragraphs, wrapping lines at 80 character columns. Check the man page for fmt for other options. Obviously, you can go nuts here by piping the text through emacs or markdown or whatever.
  5. Save the service with a sensible name like "Reformat-to-80"

Now, when you have text selected in Xcode, you will be able to reformat it to 80 characters by selecting the service via the menu bar: Xcode / Services / Reformat-to-80.

Now let's assign a keyboard shortcut to it:

  1. Open the Keyboard Preference Pane, go to the Keyboard Shortcuts tab.
  2. Select Application Shortcuts in the left pane.
  3. In the right pane, using the add button (the +) to add a new shortcut for the copy of Xcode you selected earlier. Type in the exact name you used for the service and add a keyboard shortcut of your choice.

Now, in Xcode, you can use that keyboard shortcut to replace the selected text with the output of the shell script, which will reformat the text to

还给你自由 2024-12-10 18:54:10

显然,你可以通过 emacs 管道传输文本来发疯

多年来,我一直期待 Emacs 在重排注释时具有先进的特性,我做到了。假设您可以克服 Xcode 调用 Emacs 进行脚本编写的麻烦,请将以下脚本保存为 fill-region (或类似的名称)并使其可执行。

#!/usr/bin/env emacs --script
(with-temp-buffer
  ;; Would like to implement --help but Emacs eats that before I can
  ;; process it here.
  (let ((param (nth 0 command-line-args-left)))
    (if param (funcall (intern param))))

  (condition-case nil
      (while t
        (insert (read-from-minibuffer ""))
        (insert "\n"))
    (error nil))
  (fill-region (point-min) (point-max))
  (princ (buffer-string)))

然后创建一个 Automator 服务,该服务使用适当的语言参数(例如 C/C++/Objective-C 的 fill-region c++-mode)执行此脚本并替换所选文本。

即使您不使用 Emacs,这也是上面使用 fmt 的直接替代品,它在注释格式方面要好得多。

Obviously, you can go nuts here by piping the text through emacs

After many years of expecting Emacs's advanced idiosyncrasies when reflowing comments, I did. Assuming you can get over the grossness of Xcode calling out to Emacs for scripting, save the following script as fill-region (or something similar) and make it executable.

#!/usr/bin/env emacs --script
(with-temp-buffer
  ;; Would like to implement --help but Emacs eats that before I can
  ;; process it here.
  (let ((param (nth 0 command-line-args-left)))
    (if param (funcall (intern param))))

  (condition-case nil
      (while t
        (insert (read-from-minibuffer ""))
        (insert "\n"))
    (error nil))
  (fill-region (point-min) (point-max))
  (princ (buffer-string)))

Then create an Automator service that executes this script with the appropriate language argument (e.g. fill-region c++-mode for C/C++/Objective-C) and replaces the selected text.

Even if you don't use Emacs, this is a drop-in replacement for the use of fmt above that's considerably better at comment formatting.

香草可樂 2024-12-10 18:54:10

添加到 user797758 的答案:要自定义他的 emacs 脚本包裹文本的列,请添加 set-fill-column 调用在填充区域调用之前。例如,下面的脚本将在 100 个字符处换行。

#!/usr/bin/env emacs --script
(with-temp-buffer
  (let ((param (nth 0 command-line-args-left)))
    (if param (funcall (intern param))))

  (condition-case nil
      (while t
        (insert (read-from-minibuffer ""))
        (insert "\n"))
    (error nil))
  (set-fill-column 100)
  (fill-region (point-min) (point-max))
  (princ (buffer-string)))

Adding to user797758's answer: To customize the column at which his emacs's script wraps your text, add a set-fill-column call before the fill-region call. For example, the below script will wrap at 100 characters.

#!/usr/bin/env emacs --script
(with-temp-buffer
  (let ((param (nth 0 command-line-args-left)))
    (if param (funcall (intern param))))

  (condition-case nil
      (while t
        (insert (read-from-minibuffer ""))
        (insert "\n"))
    (error nil))
  (set-fill-column 100)
  (fill-region (point-min) (point-max))
  (princ (buffer-string)))
安人多梦 2024-12-10 18:54:10

好吧,对我来说,如何格式化内容从来都不是一个大问题(就我个人而言,我从来没有评论过大量文本),但我认为这里有一些东西非常有用。
要注释掉代码块,您可以快速注释掉代码块,如下所示:

Select one or more lines of code to comment
    Command-/

Well it has never been a big deal to me how things are formatted, (personally, and I never have large amounts of text commented) but here is something I think you will find to be very useful.
To Comment Out a Block of Code, you can quickly comment out a block of code as follows:

Select one or more lines of code to comment
    Command-/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文