gpt4 book ai didi

haskell - 选定文本上的 Tab 和 Shift-Tab 智能缩进

转载 作者:行者123 更新时间:2023-12-03 23:32:25 25 4
gpt4 key购买 nike

在大多数现代编辑器中,您可以突出显示一段代码并缩进或取消缩进制表符或您使用的任何空格,这是大多数现代编辑器的标准;你如何在 emacs 中做到这一点?

因此,例如我刚刚打开 sublime text,突出显示以下代码:

variation1 person phoneMap carrierMap addressMap =
case M.lookup person phoneMap of
Nothing -> Nothing
Just number ->
case M.lookup number carrierMap of
Nothing -> Nothing
Just carrier -> M.lookup carrier addressMap

然后按下选项卡并得到

  variation1 person phoneMap carrierMap addressMap =
case M.lookup person phoneMap of
Nothing -> Nothing
Just number ->
case M.lookup number carrierMap of
Nothing -> Nothing
Just carrier -> M.lookup carrier addressMap

该代码上的一个 shift-tab 将其返回到原来的位置,如果我继续按 shift-tab,我最终会得到以下信息:

variation1 person phoneMap carrierMap addressMap =
case M.lookup person phoneMap of
Nothing -> Nothing
Just number ->
case M.lookup number carrierMap of
Nothing -> Nothing
Just carrier -> M.lookup carrier addressMap

引用另一个回复:

emacs language modes don't really have a notion of 'indent this block 1 tab further'. Instead they're very opinionated and have a notion of 'this is the correct indentation' and that's what you get when you hit tab in a language mode.



除非我使用以下代码(启用了haskell模式和ghc mod)执行此操作:

import Monad
import System
import IO
import Random
import Control.Monad.State

type RandomState a = State StdGen a
data CountedRandom = CountedRandom {
crGen :: StdGen
, crCount :: Int
}

type CRState = State CountedRandom

getRandom :: Random a => RandomState a
getRandom =
get >>= \gen ->
let (val, gen') = random gen in
put gen' >>
return val

我得到以下信息:

import Monad
import System
import IO
import Random
import Control.Monad.State

type RandomState a = State StdGen a
data CountedRandom = CountedRandom {
crGen :: StdGen
, crCount :: Int
}

type CRState = State CountedRandom

getRandom :: Random a => RandomState a
getRandom =
get >>= \gen ->
let (val, gen') = random gen in
put gen' >>
return val

当我想要

import Monad
import System
import IO
import Random
import Control.Monad.State

type RandomState a = State StdGen a
data CountedRandom = CountedRandom {
crGen :: StdGen
, crCount :: Int
}

type CRState = State CountedRandom

getRandom :: Random a => RandomState a
getRandom =
get >>= \gen ->
let (val, gen') = random gen in
put gen' >>
return val

足够接近ataylor的解决方案:

(defcustom tab-shift-width 4
"Sets selected text shift width on tab"
:type 'integer)
(make-variable-buffer-local 'tab-shift-width)

(global-set-key
(kbd "<tab>")
(lambda (start end)
(interactive "r")
(if (use-region-p)
(save-excursion
(let ((deactivate-mark nil))
(indent-rigidly start end tab-shift-width)))
(indent-for-tab-command))))

(global-set-key
(kbd "S-<tab>")
(lambda (start end)
(interactive "r")
(if (use-region-p)
(save-excursion
(let ((deactivate-mark nil))
(indent-rigidly start end (- tab-shift-width))))
(indent-for-tab-command))))

如果 emacs 支持缩进检测(即,只需获取某个变量的值),那就太好了;我发现与此最接近的是一个名为 dtrt indent 的插件,但它不适用于 Haskell。

最佳答案

indent-region将根据当前模式重新缩进文本块。

要强制添加缩进级别,您可以使用 string-rectangle ,这将提示您输入一个字符串。您可以在此处提供缩进级别的字符串(例如制表符、4 个空格等)。字符串将插入当前选定区域的每一行,在当前列中,有效地缩进它。或者,您可以从 open-rectangle 获得类似的效果。 ,这将在矩形中插入空白,角由点和标记定义。

另一种强制缩进的方法是调用 indent-rigidly (C-x TAB)。这将覆盖特定于模式的缩进规则并缩进固定数量。数字参数指定缩进多少,负参数将取消缩进。如果您希望这是选择区域时的默认行为,您可以执行以下操作:

(global-set-key 
(kbd "<tab>")
(lambda (start end)
(interactive "r")
(if (use-region-p)
(save-excursion
(let ((deactivate-mark nil))
(indent-rigidly start end 4)))
(indent-for-tab-command))))

关于haskell - 选定文本上的 Tab 和 Shift-Tab 智能缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10487726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com