Skip to content

Instantly share code, notes, and snippets.

@vvv
vvv / clangd.md
Created January 8, 2026 19:38 — forked from Strus/clangd.md
How to use clangd C/C++ LSP in any project

How to use clangd C/C++ LSP in any project

tl;dr: If you want to just know the method, skip to How to section

Clangd is a state-of-the-art C/C++ LSP that can be used in every popular text editors like Neovim, Emacs or VS Code. Even CLion uses clangd under the hood. Unfortunately, clangd requires compile_commands.json to work, and the easiest way to painlessly generate it is to use CMake.

For simple projects you can try to use Bear - it will capture compile commands and generate compile_commands.json. Although I could never make it work in big projects with custom or complicated build systems.

But what if I tell you you can quickly hack your way around that, and generate compile_commands.json for any project, no matter how compilcated? I have used that way at work for years, originaly because I used CLion which supported only CMake projects - but now I use that method succesfully with clangd and Neovim.

Commit Message Guidelines

Short (72 chars or less) summary

More detailed explanatory text. Wrap it to 72 characters. The blank
line separating the summary from the body is critical (unless you omit
the body entirely).

Write your commit message in the imperative: "Fix bug" and not "Fixed
bug" or "Fixes bug." This convention matches up with commit messages
#!/usr/bin/env stack
{- stack --resolver lts-9.21 script
--package shelly --package text
-}
{-# OPTIONS_GHC -Wall -Werror #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
import Shelly
#!/usr/bin/env stack
-- stack --resolver lts-9.21 script --package loch-th
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wall -Werror #-}
import Debug.Trace.LocationTH (check)
import System.Environment (getArgs)
main :: IO ()
main = do
@vvv
vvv / applicative-something.hs
Created March 28, 2018 18:37
Looking for idiomatic way to write expression with `Applicative`
#!/usr/bin/env stack
-- stack --resolver lts-11.2 script
{-# OPTIONS_GHC -Wall -W -Werror #-}
data Foo = Foo
{ fooInt :: Int
, fooBar :: Bar
} deriving Show
data Bar = Bar Baz
@vvv
vvv / DaPhone.hs
Created December 2, 2017 00:02
The Phone exercise from Chapter 11 of the Haskell book
{-# OPTIONS_GHC -Wall -Werror #-}
module DaPhone where
import Data.Char (isUpper, toLower)
import Data.List (elemIndex, foldl', intersperse)
import Data.Maybe (catMaybes)
data DaPhone = DaPhone [String]
deriving (Eq, Show)
@vvv
vvv / OrdEq.hs
Created September 7, 2017 10:54 — forked from mssawant/ordeq.hs
import Data.Foldable (foldl')
import Debug.Trace (trace)
prnEqual :: (Eq a) => a -> a -> IO ()
prnEqual a b = if a == b then print "True" else print "False"
-- Note the absence of parentheses around `Eq a` and having exactly one `print`.
prnEqual' :: Eq a => a -> a -> IO ()
prnEqual' a b = print $ if a == b then "True" else "False"
@vvv
vvv / misc.hs
Last active August 24, 2017 16:50 — forked from mssawant/misc.hs
Haskell exercise
{-- XXX Your definition of `checkChar` is okay, but the abstraction itself
-- is very odd. It is fine as an exercise of using "do" notation and
-- `return`. In real code though explicit statements are both short and
-- expressive (see lines 21-22).
checkChar :: IO Bool
checkChar = do
c <- getChar
return (c == 'y')
--}
listsum :: Num a => [a] -> a
listsum [] = 0
listsum (x:xs) = x + listsum xs
main :: IO ()
main = print $ listsum [1..5]
@vvv
vvv / Program.hs
Last active July 13, 2017 18:50
Working through the Operational Monad Tutorial
{-# LANGUAGE GADTs #-}
module Program where
--
-- The Operational Monad Tutorial:
-- http://apfelmus.nfshost.com/articles/operational-monad.html
--
-- GADT
data Program instr a where
Then :: instr a -> (a -> Program instr b) -> Program instr b