Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Makefile): enable auto detect compiler #1282

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .github/data/project-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,3 @@ there'd
truthy
varargs
vsnip
Unix
MinGW
CC's
usr
gcc
exe
MSYS
SHELLFLAGS
30 changes: 13 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ nvim: | ${NVIM_PATH}
git -C ${NVIM_MASTER_PATH} fetch origin master --depth 1
git -C ${NVIM_MASTER_PATH} checkout FETCH_HEAD

LUASNIP_DETECTED_OS?=$(shell uname 2>/dev/null)
LUASNIP_DETECTED_OS?=$(shell uname;)
ifeq ($(LUASNIP_DETECTED_OS),Darwin)
# flags for dynamic linking on macos, from luarocks
# (https://github.com/luarocks/luarocks/blob/9a3c5a879849f4f411a96cf1bdc0c4c7e26ade42/src/luarocks/core/cfg.lua#LL468C37-L468C80)
Expand All @@ -47,23 +47,17 @@ ifeq ($(OS_ENV),Windows_NT)
LUASNIP_DETECTED_OS:=Windows
endif
# Example output on Windows: MINGW64_NT-10.0-19045 DESKTOP-ABCDE 3.4.10-1234567.x86_64 2024-02-14 20:17 UTC x86_64 Msys
UNAME_ALL:=$(shell uname -a 2>/dev/null)
UNAME_ALL:=$(shell uname -a;)
ifneq (,$(findstring MINGW,$(UNAME_ALL)))
LUASNIP_DETECTED_OS:=Windows
endif
ifneq (,$(findstring Msys,$(UNAME_ALL)))
LUASNIP_DETECTED_OS:=Windows
endif

# On Windows, you may need to set:
# SHELL=C:/path/to/Git/usr/bin/sh.exe
# .SHELLFLAGS=-c
# CC=gcc
# NEOVIM_BIN_PATH=C:/path/to/Neovim/bin # contains lua51.dll, or use your own LUA_LDLIBS
ifeq ($(LUASNIP_DETECTED_OS),Windows)
# If neovim is installed by scoop, only scoop/shims is exposed. We need to find original nvim/bin that contains lua51.dll
# If neovim is installed by winget or other methods, nvim/bin is already included in PATH.
# Double quotes the absolute path if it contains spaces

# `scoop prefix neovim` outputs either
# 1. C:\Users\MyUsername\scoop\apps\neovim\current
Expand All @@ -73,29 +67,31 @@ ifeq ($(LUASNIP_DETECTED_OS),Windows)
# The following code will also work if future scoop returns 1 for unknown `package`
#
# On Git Bash, `which nvim` returns a Unix style path: `/c/Program Files/Git/bin/nvim`
# Convertion to `"C:/Program Files/Git/bin/nvim"` may be needed if neovim is running in powershell or pwsh
# Always convert to `C:/Program Files/Git/bin/nvim` for powershell and pwsh users
NEOVIM_BIN_PATH?=$(shell \
if (scoop prefix neovim | grep '^[A-Z]:[/\\]') >/dev/null 2>&1; then \
echo "$$(scoop prefix neovim)/bin" | sed 's/\\\\/\\//g' | sed 's/\\(.*\\) \\(.*\\)/"\\1 \\2"/'; \
if (scoop prefix neovim | grep '^[A-Z]:[/\\\\]') >/dev/null 2>&1; then \
echo "$$(scoop prefix neovim)/bin" | sed 's/\\\\/\\//g'; \
elif which nvim >/dev/null 2>&1; then \
dirname "$$(which nvim)" | sed 's/^\\/\\(.\\)\\//\\U\\1:\\//' | sed 's/\\(.*\\) \\(.*\\)/"\\1 \\2"/'; \
dirname "$$(which nvim)" | sed 's/^\\/\\(.\\)\\//\\U\\1:\\//'; \
fi)

LUA_LDLIBS?=$(if $(strip $(NEOVIM_BIN_PATH)),-L$(NEOVIM_BIN_PATH) -llua51,)
# Always double quote the absolute path as it may contain spaces
LUA_LDLIBS?=$(if $(strip $(NEOVIM_BIN_PATH)),-L"$(NEOVIM_BIN_PATH)" -llua51,)
endif

PROJECT_ROOT:=$(shell pwd 2>/dev/null)
CC_ENV:=$(shell (which $(CC) || which clang || which gcc || which cl || which zig) 2>/dev/null;)
PROJECT_ROOT:=$(CURDIR)
JSREGEXP_PATH=$(PROJECT_ROOT)/deps/jsregexp
JSREGEXP005_PATH=$(PROJECT_ROOT)/deps/jsregexp005
jsregexp:
git submodule init
git submodule update
"$(MAKE)" "CC=$(CC)" "INCLUDE_DIR=-I$(PROJECT_ROOT)/deps/lua51_include/" LDLIBS='$(LUA_LDLIBS)' -C "$(JSREGEXP_PATH)"
"$(MAKE)" "CC=$(CC)" "INCLUDE_DIR=-I$(PROJECT_ROOT)/deps/lua51_include/" LDLIBS='$(LUA_LDLIBS)' -C "$(JSREGEXP005_PATH)"
"$(MAKE)" "CC=$(CC_ENV)" "INCLUDE_DIR=-I$(PROJECT_ROOT)/deps/lua51_include/" 'LDLIBS=$(LUA_LDLIBS)' -C "$(JSREGEXP_PATH)"
"$(MAKE)" "CC=$(CC_ENV)" "INCLUDE_DIR=-I$(PROJECT_ROOT)/deps/lua51_include/" 'LDLIBS=$(LUA_LDLIBS)' -C "$(JSREGEXP005_PATH)"

install_jsregexp: jsregexp
# remove old binary.
rm "$(PROJECT_ROOT)/lua/luasnip-jsregexp.so" || true
rm -f "$(PROJECT_ROOT)/lua/luasnip-jsregexp.so"
# there is some additional trickery to make this work with jsregexp-0.0.6 in
# util/jsregexp.lua.
cp "$(JSREGEXP_PATH)/jsregexp.lua" "$(PROJECT_ROOT)/lua/luasnip-jsregexp.lua"
Expand Down
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,11 @@ Neovim >= 0.7 (extmarks)
Consider watching the repository's releases so you're notified when a new version becomes available.

> [!NOTE]
> On Windows, you need to use a shell that can run Unix commands (MinGW,MSYS2,etc).
> Luckily, Git offers a `sh.exe`, so you don't need to install a heavy MSYS2 environment.
> Other than Git, you also need a C compiler and `make` to install `jsregexp`.
> You may also need to change the build command: `make install_jsregexp CC=gcc.exe SHELL=C:/path/to/sh.exe .SHELLFLAGS=-c`:

```text
SHELL=C:/path/to/Git/usr/bin/sh.exe # if Git/MinGW/MSYS2 `sh.exe` is not in PATH
.SHELLFLAGS=-c # if Git/MinGW/MSYS2 `sh.exe` is not in PATH
CC=gcc.exe # if CC's default value cc is not set (when `which cc` fails to find the compiler command)
NEOVIM_BIN_PATH=C:/path/to/Neovim/bin # if the Makefile fails to automatically detect the Neovim/bin path
```
> On Windows, you need a C compiler and `make` to install `jsregexp`. If the
> compiler is not `gcc` or `clang`, you need to explicitly specify the `CC`
> flag in the build command: `make install_jsregexp CC=your_compiler_program`.
> Also, make sure `%GIT%/bin` directory is added in the `$PATH` so that
> `make` can use `%GIT%/bin/sh.exe`.

## Keymaps
In Vim script, with `<Tab>` for jumping forward/expanding a snippet, `<Shift-Tab>` for
Expand Down Expand Up @@ -127,9 +121,9 @@ loaders and their benefits. The following list serves only as a short overview.
```lua
-- load snippets from path/of/your/nvim/config/my-cool-snippets
require("luasnip.loaders.from_vscode").lazy_load({ paths = { "./my-cool-snippets" } })

(Note: It's mandatory to have a 'package.json' file in the snippet directory. For examples, see documentation.)
```
> NOTE:
> It's mandatory to have a `package.json` file in the snippet directory. For examples, see [friendly-snippets](https://github.com/rafamadriz/friendly-snippets/blob/main/package.json).
For more info on the VS Code loader, check the [examples](https://github.com/L3MON4D3/LuaSnip/blob/b5a72f1fbde545be101fcd10b70bcd51ea4367de/Examples/snippets.lua#L501) or [documentation](https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md#loaders).

- **SnipMate-like**: Very similar to VS Code packages; install a plugin that provides snippets and call the `load`-function:
Expand All @@ -139,7 +133,7 @@ loaders and their benefits. The following list serves only as a short overview.
The SnipMate format is very simple, so adding **custom snippets** only requires a few steps:
- add a directory beside your `init.vim` (or any other place that is in your `runtimepath`) named `snippets`.
- inside that directory, create files named `<filetype>.snippets` and add snippets for the given filetype in it (for inspiration, check [honza/vim-snippets](https://github.com/honza/vim-snippets/tree/master/snippets)).
``` snipmate
```snipmate
# comment
snippet <trigger> <description>
<snippet-body>
Expand Down
2 changes: 1 addition & 1 deletion doc/luasnip.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*luasnip.txt* For NVIM v0.8.0 Last change: 2025 January 04
*luasnip.txt* For NVIM v0.8.0 Last change: 2025 January 27

==============================================================================
Table of Contents *luasnip-table-of-contents*
Expand Down