Search code examples
visual-studio-codehaskellcode-editor

In VS Code what's the quickest way to replace a symbol on two consecutive lines when the proper "Rename Symbol" command does not work?


In VS Code (with the Haskell extension and HLS installed) I have the following Haskell file:

module Stuff where

myLen :: [a] -> Int
myLen list = case list of
  [] -> 0
  x:xs -> 1 + myLen xs


myLenx :: [a] -> Int
myLenx = foldr (\x y -> y + 1) 0

In this file I want to change the symbol myLenx to myLen'. When using the 'Rename symbol' command (default keybinding F2) I get always an error message like "Explicit export required for Renaming" or "No AST for file: NormalizedFilePath [...]/Stuff.hs".

The reason seems to be, the Haskell language Server (HLS) cannot handle renaming for non exported symbols. But I don't want to add exports just for renaming some function.

So, is there a way to quickly change a symbol in two consecutive lines (that's what we usually have in Haskell for (top-level) function definitions)? Is there a extension which helps with this (maybe in a language-independent way)?


Solution

  • In this particular case, with the two aligning vertically, you can use column/box-selection (multi-cursor editing). Hold shift+alt while dragging the cursor. Even if they don't align vertically, you can create multiple cursors anywhere: hold alt and click each location where you want to add a cursor. Then delete and add text as you wish.

    You can also use the find and replace feature, which you can do per-editor, or across multiple files (in the Search view). There's also a regex mode to VS Code's find and replace, which is useful for more complicated scenarios. You can also select specific text which you want to constrain the find and replace to only apply within (see the "find in selection" icon (/))