├── .clang-format ├── LICENSE ├── logos-format.py └── README.md /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Chromium 2 | Language: ObjC 3 | IndentWidth: 4 4 | TabWidth: 4 5 | UseTab: Never 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Hearse 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /logos-format.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | from subprocess import Popen, PIPE, STDOUT 4 | 5 | # block level 6 | # hook -> replace with @logosformathook with ; at the end 7 | # end -> replace with @logosformatend with ; at the end 8 | # property -> replace with @logosformatproperty with NO ; at the end. Special case for block level 9 | # new -> replce with @logosformatnew with ; at the end 10 | # group -> replace with @logosformatgroup with ; at the end 11 | # subclass -> replace with @logosformatsubclass with ; at the end 12 | # top level 13 | # config -> replace with @logosformatconfig 14 | # hookf -> replace with @logosformathookf 15 | # ctor -> replace with @logosformatctor 16 | # dtor -> replace with @logosformatdtor 17 | 18 | # function level 19 | # init -> replace with @logosformatinit 20 | # c -> replace with @logosformatc 21 | # orig -> replace with @logosformatorig 22 | # log -> replace with @logosformatlog 23 | 24 | specialFilterList = ["%hook", "%end", "%new", "%group", "%subclass"] 25 | filterList = [ 26 | "%property", 27 | "%config", 28 | "%hookf", 29 | "%ctor", 30 | "%dtor", 31 | "%init", 32 | "%c", 33 | "%orig", 34 | "%log", 35 | ] 36 | 37 | 38 | fileContentsList = sys.stdin.read().splitlines() 39 | newList = [] 40 | 41 | for line in fileContentsList: 42 | for token in filterList: 43 | if token in line: 44 | line = re.sub(rf"%({token[1:]})\b", r"@logosformat\1", line) 45 | for token in specialFilterList: 46 | if token in line: 47 | line = re.sub(rf"%({token[1:]})\b", r"@logosformat\1", line) + ";" 48 | newList.append(line) 49 | 50 | command = ["clang-format"] + sys.argv[1:] 51 | process = Popen(command, stdout=PIPE, stderr=None, stdin=PIPE) 52 | stdoutData = process.communicate(input="\n".join(newList).encode())[0] 53 | refinedList = stdoutData.decode().splitlines() 54 | 55 | 56 | for line in refinedList: 57 | if "@logosformat" in line: 58 | fix = line.replace("@logosformat", "%") 59 | if any(token in fix for token in specialFilterList): 60 | print(fix.replace(";","")) 61 | else: 62 | print(fix) 63 | else: 64 | print(line) 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # logos-format 2 |

logos-format is a project that allows for logos to be formatted with the help of clang-format

3 | 4 | Before: 5 | 6 | ![image](https://user-images.githubusercontent.com/28695977/145279771-8631da6e-179a-4d19-bf1e-ed1fce304ffe.png) 7 | 8 | 9 | After(BasedOnStyle: Chromium): 10 | 11 | ![image](https://user-images.githubusercontent.com/28695977/145279821-8dba8eee-0b7c-46c9-8ebf-3df6881e568a.png) 12 | 13 | 14 | 15 |

Features

16 | 17 | - Uses clang-format to format, therefore performs exactly like it. 18 | - You can also specify arguments to be passed to clang-format. 19 | - Check out https://clang.llvm.org/docs/ClangFormatStyleOptions.html for possible options. 20 | - There is an example .clang-format file provided in this project as a starting point. 21 | - You do not need a .clang-format file, although clang-format will look through your project directory structure to see if you do have one present. 22 | - An example .clang-format file would look something like this, I picked the style chromium because of their extensive docs 23 | 24 | ``` 25 | BasedOnStyle: Chromium 26 | ... 27 | ``` 28 | 29 | 30 | Check out more about Chromium Objective-C style guide here https://google.github.io/styleguide/objcguide.html 31 | 32 | 33 | 34 | 35 |

How to use

36 | 37 | - clang-format is required to be installed on your machine
38 | On Macs: 39 | 40 | ``` 41 | brew install clang-format 42 | ``` 43 | - python3 is required to be installed on your machine
44 | On Macs: 45 | 46 | ``` 47 | brew install python 48 | ``` 49 | - Download logos-format.py and place in desired directory
50 | `curl -JLO https://github.com/HearseDev/logos-format/raw/main/logos-format.py`
51 | 52 | 53 |

To use it with CLI

54 | 55 | ``` 56 | python3 /path/to/logos-format.py --assume-filename objc < /path/to/Tweak.xm 57 | ``` 58 | 59 |

To use it with VSCode

60 | For now this is just a method for testing
61 | 62 | - You need to have the logos extension by aarnav installed 63 | https://marketplace.visualstudio.com/items?itemName=tale.logos-vscode 64 | 65 | 66 | - Install External formatters from the marketplace 67 | https://marketplace.visualstudio.com/items?itemName=SteefH.external-formatters 68 | 69 | - Add this code to your settings.json 70 | ``` 71 | "externalFormatters.languages": { 72 | "logos": { 73 | "command": "python3", 74 | "arguments": [ 75 | "/path/to/logos-format.py", 76 | "--assume-filename", 77 | "objc", 78 | ] 79 | }, 80 | } 81 | ``` 82 | - You can format using the default VSCode option to format, you can have it format on save as well 83 | 84 | 85 |

To use it with neovim

86 | 87 | - Install this plugin https://github.com/mhartington/formatter.nvim 88 | 89 | - Add this to your init.vim 90 | ``` 91 | autocmd BufNewFile,BufRead *.xm,*.x :set filetype=logos 92 | nnoremap f :Format 93 | lua << EOF 94 | require('formatter').setup { 95 | filetype = { 96 | logos = { 97 | -- clang-format 98 | function() 99 | return { 100 | --change this 101 | exe = 'python3', 102 | args = {"/path/to/logos-format.py","--assume-filename", "objc"}, 103 | stdin = true, 104 | cwd = vim.fn.expand '%:p:h', -- Run clang-format in cwd of the file. 105 | } 106 | end, 107 | }, 108 | }, 109 | } 110 | EOF 111 | ``` 112 | - Now you can format the file using your keybind, in this case, using space + f 113 | 114 |

To use with others

115 | 116 | - Logos-format works with any text editor that allows the use of external formatters. This is because logos-format works like any traditional formatter, it reads input from stdin and spits out formatted code to stdout. Just look up how to set up an external formatter for your text editor. I will be adding popular options here once i have finished testing. 117 | 118 |

Special Thanks

119 | 120 | - uroboro for his immense help 121 | --------------------------------------------------------------------------------