Skip to content

Commit

Permalink
Add support for building CMake projects with ctrl-space + release flag
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Feb 3, 2025
1 parent ddfa0bc commit 4b4b37a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 26 deletions.
3 changes: 3 additions & 0 deletions o.1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Clear all file locks.
Use Ollama for tab completion with the model specified in the \fBORBITON_OLLAMA\fP environment variable. This requires that Ollama is up and running,
either locally or at the host specified in the \fBOLLAMA_HOST\fP environment variable.
.TP
.B \-r or \-\-release
Build with release instead of debug mode whenever applicable. This can be useful when editing a CMakeLists.txt file and then pressing ctrl-space to build the project.
.TP
.B \-b or \-\-bat
List the file with syntax highlighting using the \fBbat\fP command, if installed.
.TP
Expand Down
46 changes: 42 additions & 4 deletions v2/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (e *Editor) exeName(sourceFilename string, shouldExist bool) string {
return exeFirstName
}

func has(executableInPath string) bool {
return files.WhichCached(executableInPath) != ""
}

// GenerateBuildCommand will generate a command for building the given filename (or for displaying HTML)
// If there are no errors, a exec.Cmd is returned together with a function that can tell if the build
// produced an executable, together with the executable name,
Expand Down Expand Up @@ -156,10 +160,45 @@ func (e *Editor) GenerateBuildCommand(c *vt100.Canvas, tty *vt100.TTY, filename
}

switch filepath.Base(sourceFilename) {
case "PKGBUILD":
has := func(s string) bool {
return files.WhichCached(s) != ""
case "CMakeLists.txt":
var s string
if has("cmake") {
if has("ninja") {
s = "cmake -B build -D CMAKE_BUILD_TYPE=Debug -G Ninja -S . -W no-dev || (rm -rv build; cmake -B build -D CMAKE_BUILD_TYPE=Debug -G Ninja -S . -W no-dev) && ninja -C build"
} else if has("make") {
s = "cmake -B build -D CMAKE_BUILD_TYPE=Debug -S . -W no-dev || (rm -rv build; cmake -B build -D CMAKE_BUILD_TYPE=Debug -S . -W no-dev) && make -C build"
} else if has("gmake") {
s = "cmake -B build -D CMAKE_BUILD_TYPE=Debug -S . -W no-dev || (rm -rv build; cmake -B build -D CMAKE_BUILD_TYPE=Debug -S . -W no-dev) && gmake -C build"
}
}
lastCommand, err := readLastCommand()
if releaseBuildFlag {
if err == nil && strings.Contains(lastCommand, "CMAKE_BUILD_TYPE=Debug ") {
s = "rm -r build; " + s
}
s = strings.ReplaceAll(s, "CMAKE_BUILD_TYPE=Debug ", "CMAKE_BUILD_TYPE=Release ")
} else {
if err == nil && strings.Contains(lastCommand, "CMAKE_BUILD_TYPE=Release ") {
s = "rm -r build; " + s
}
}
if s != "" {
// Save and exec / replace the process with syscall.Exec
if e.Save(c, tty) == nil { // success
// Unlock and save the lock file
if absFilename, err := filepath.Abs(e.filename); fileLock != nil && err == nil { // success
fileLock.Unlock(absFilename)
fileLock.Save()
}
quitExecShellCommand(tty, sourceDir, s) // The program ends here
}
// Could not save the file, execute the command in a separate process
args := strings.Split(s, " ")
cmd = exec.Command(args[0], args[1:]...)
cmd.Dir = sourceDir
return cmd, everythingIsFine, nil
}
case "PKGBUILD":
var s string
if has("tinyionice") {
s += "tinyionice "
Expand All @@ -184,7 +223,6 @@ func (e *Editor) GenerateBuildCommand(c *vt100.Canvas, tty *vt100.TTY, filename
}
quitExecShellCommand(tty, sourceDir, s) // The program ends here
}

// Could not save the file, execute the command in a separate process
args := strings.Split(s, " ")
cmd = exec.Command(args[0], args[1:]...)
Expand Down
1 change: 1 addition & 0 deletions v2/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Flags:
-m, --monitor FILENAME Monitor the given file for changes, and open it as read-only.
-o, --ollama Use $OLLAMA$
for tab completion (experimental feature).
-r, --release Build with release instead of debug mode whenever applicable.
-x, --noapprox Disable approximate filename matching.
-n, --no-cache Avoid writing the location history, search history, highscore,
compilation and format command to ` + cacheDirForDoc + `.
Expand Down
48 changes: 26 additions & 22 deletions v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,51 +54,55 @@ var (

// The Ollama model that is used for code completion
codeCompletionModel string

// Build with release mode instead of debug mode whenever applicable
releaseBuildFlag bool
)

func main() {
var (
batFlag bool
buildFlag bool
catFlag bool
clearLocksFlag bool
copyFlag bool
createDirectoriesFlag bool
forceFlag bool
formatFlag bool
helpFlag bool
lastCommandFlag bool
listDigraphsFlag bool
monitorAndReadOnlyFlag bool
nanoMode bool
noApproxMatchFlag bool
noCacheFlag bool
ollamaTabCompletion bool
pasteFlag bool
clearLocksFlag bool
lastCommandFlag bool
quickHelpFlag bool
createDirectoriesFlag bool
versionFlag bool
nanoMode bool
ollamaTabCompletion bool
catFlag bool
batFlag bool
formatFlag bool
buildFlag bool
noApproxMatchFlag bool
listDigraphsFlag bool
)

pflag.BoolVarP(&batFlag, "bat", "B", false, "Cat the file with colors instead of editing it, using bat")
pflag.BoolVarP(&buildFlag, "build", "b", false, "Try to build the file instead of editing it")
pflag.BoolVarP(&catFlag, "list", "t", false, "List the file with colors instead of editing it")
pflag.BoolVarP(&clearLocksFlag, "clear-locks", "r", false, "clear all file locks")
pflag.BoolVarP(&copyFlag, "copy", "c", false, "copy a file into the clipboard and quit")
pflag.BoolVarP(&createDirectoriesFlag, "create-dir", "d", false, "create diretories when opening a new file")
pflag.BoolVarP(&forceFlag, "force", "f", false, "open even if already open")
pflag.BoolVarP(&formatFlag, "format", "F", false, "Try to build the file instead of editing it")
pflag.BoolVarP(&helpFlag, "help", "h", false, "quick overview of hotkeys and flags")
pflag.BoolVarP(&lastCommandFlag, "last-command", "l", false, "output the last build or format command")
pflag.BoolVarP(&listDigraphsFlag, "digraphs", "g", false, "List digraphs")
pflag.BoolVarP(&monitorAndReadOnlyFlag, "monitor", "m", false, "open read-only and monitor for changes")
pflag.BoolVarP(&nanoMode, "nano", "a", false, "Nano/Pico mode")
pflag.BoolVarP(&noApproxMatchFlag, "noapprox", "x", false, "Disable approximate filename matching")
pflag.BoolVarP(&noCacheFlag, "no-cache", "n", false, "don't write anything to cache directory")
pflag.BoolVarP(&ollamaTabCompletion, "ollama", "o", env.Bool("ORBITON_OLLAMA"), "use Ollama for tab completion")
pflag.BoolVarP(&pasteFlag, "paste", "p", false, "paste the clipboard into the file and quit")
pflag.BoolVarP(&clearLocksFlag, "clear-locks", "r", false, "clear all file locks")
pflag.BoolVarP(&lastCommandFlag, "last-command", "l", false, "output the last build or format command")
pflag.BoolVarP(&releaseBuildFlag, "release", "r", false, "build with release mode instead of debug mode, whenever applicable")
pflag.BoolVarP(&quickHelpFlag, "quick-help", "q", false, "always display the quick help when starting")
pflag.BoolVarP(&createDirectoriesFlag, "create-dir", "d", false, "create diretories when opening a new file")
pflag.BoolVarP(&versionFlag, "version", "v", false, "version information")
pflag.StringVarP(&inputFileWhenRunning, "input-file", "i", "input.txt", "input file when building and running programs")
pflag.BoolVarP(&nanoMode, "nano", "a", false, "Nano/Pico mode")
pflag.BoolVarP(&ollamaTabCompletion, "ollama", "o", env.Bool("ORBITON_OLLAMA"), "use Ollama for tab completion")
pflag.BoolVarP(&catFlag, "list", "t", false, "List the file with colors instead of editing it")
pflag.BoolVarP(&batFlag, "bat", "B", false, "Cat the file with colors instead of editing it, using bat")
pflag.BoolVarP(&formatFlag, "format", "F", false, "Try to build the file instead of editing it")
pflag.BoolVarP(&buildFlag, "build", "b", false, "Try to build the file instead of editing it")
pflag.BoolVarP(&noApproxMatchFlag, "noapprox", "x", false, "Disable approximate filename matching")
pflag.BoolVarP(&listDigraphsFlag, "digraphs", "g", false, "List digraphs")

pflag.Parse()

Expand Down

0 comments on commit 4b4b37a

Please sign in to comment.