C/C++ ClangFormat Setup

Find Tips and tricks on how to better use the Zeus IDE. Feel free to post your own tips but please do not post bug reports, feature requests or questions here.
Post Reply
Site Admin
Posts: 2491
Joined: Fri Aug 13, 2004 5:10 pm

C/C++ ClangFormat Setup

Post by jussij »

ClangFormat is a tool that can be used to format C, C++, C#, Java, JavaScript, Objective-C and Protobuf code.

The steps below describe how to configure Zeus to use this tool to automatically format C and C++ code whenever the file is saved to disk.

Clang Installation
Follow the clang installation steps found here.

To check the installation is correct, start the Zeus IDE and use the Tools, DOS Command Line menu to run the following command:

Code: Select all

clang-format --version
That command should produce the following output:

Code: Select all

clang-format version 12.0.0
If that output is not produced check the details from the installation link to make sure all the steps were carried out correctly.

Zeus Configuration
Save the Lua code shown below to the clang_format.lua file in the zScript folder found in the Zeus installation folder.

Then use the Options, Document Types menu to edit the C/C++ Document Type and in the Triggers section add the following command to Save and Save As postfix sections:

Code: Select all

NOTE: To have the tool automatically format C#, Java, JavaScript, Objective-C and Protobuf code just make the corresponding changes to those document types.

With those changes in place open a test C or C++ file, make a change to that file and then save the file to disk. That action should see the file automatically reformatted based on the style specified in the Lua script.

Cheers Jussi

Code: Select all

--        Name: Clang Reformating Macro
--    Language: Lua Macro
-- Description: This macro will execute the clang-format utility to reformat
--              the current file.
--              The macro can be bound to a key or run using the Macros menu.
--              To automatically run the macro of any file save action edit
--              the corresponding document type and in the Triggers section
--              add the following to Save and Save As postfix section:
--                  $zud\zScript\clang_format.lua
local utils = require "utils"

function key_macro()
    -- details about the active document
    local document, named, read_only, document_type = utils.document_details()

    if ( (document_type ~= "C# Document Type") and
         (document_type ~= "C/C++ Document Type") and
         (document_type ~= "Java Document Type") and
         (document_type ~= "JScript Document Type") and
         (document_type ~= "ObjectiveC Document Type") )
        or (document == 0) or (named == 0) then
            if is_trigger() == 0 then
                message("This macro only works with named, writable C, C++, C#, Java, JavaScript and Objective-C documents.")

    -- the directory of the current document
    local dir = macro_tag("$fdd")

    -- the name of the current document
    local file_name = macro_tag("$f")

    message("Running clang-format to format code....")

    -- The folowing clang-format options being used:
    --   -i               - Inplace edit <file>s, if specified.
    --   --style=<string> - Coding style, currently supports:
    --                      Chromium, Google, LLVM, Mozilla, WebKit.
    -- More details found here: https://clang.llvm.org/docs/ClangFormat.html

    --local style = "Chromium"
    --local style = "Google"
    --local style = "LLVM"
    --local style = "Mozilla"
    local style = "WebKit"

    local cmd_options = "-i --style=" .. style

    -- build up the final styling command line
    local cmd = "clang-format " .. cmd_options .. " " .. utils.checkLFN(file_name)

    -- for debugging only
    -- message_box(1, cmd, "Command Line")

    -- System Control Values
    --   1 - save the document before running the program
    --   2 - capture any standard output generated by the program
    --   4 - capture any standard error generated by the program
    --   8 - ask for additional arguments
    --  16 - the program will use the MS-DOS command interpreter (ie. dir *.* etc)
    --  32 - wait for the program to complete (the ESC key will cancel the wait)
    --  64 - run the program in a visible DOS session (otherwise runs hidden)
    -- 128 - capture output to a compiler window (not the default tool window)
    -- Output Control Flags:
    --   0 - ALWAYS
    --   1 - NEVER
    --   2 - ERRORS
    --   3 - WARNINGS
    --   4 - ANYOUTPUT

    -- display the tool window if any output is generated
    local ANYOUTPUT = 4

    -- run with 'save', 'capture output', 'run using cmd', 'wait' and 'compiler' options
    local flags = 1+2+4+16+32+128

    local caption = "clang-format: " .. file_name

    -- remember the current line count
    local current_count = get_line_count()

    if (system(cmd, dir, flags, caption, ANYOUTPUT) == 0) then
        -- reload the newly formatted docuemnt

        -- get the new line count
        local line_change = get_line_count() - current_count

        -- maintain the current line location
        if line_change ~= 0 then
            local line_current = get_line_pos() + line_change

            if line_current < 0 then line_current = 1 end


        -- no errors detected

        -- some feedback
        message("The formatting of the document is complete.")
        -- formatting errors detected

key_macro() -- run the macro
Post Reply