From cde6b6a58f3b86349200bd387dbaaff1eb656730 Mon Sep 17 00:00:00 2001 From: Campbell Barton <ideasman42@gmail.com> Date: Thu, 6 Mar 2014 22:48:04 +1100 Subject: [PATCH] add other utilities --- .../externaltools/qtc_cpp_to_c_comments.py | 65 +++++++++++++++++++ .../externaltools/qtc_cpp_to_c_comments.xml | 11 ++++ .../externaltools/qtc_expand_tabmix.py | 21 ++++++ .../externaltools/qtc_expand_tabmix.xml | 11 ++++ .../qtc_right_align_trailing_char.py | 42 ++++++++++++ .../qtc_right_align_trailing_char.xml | 11 ++++ .../qtcreator/externaltools/qtc_sort_paths.py | 35 ++++++++++ .../externaltools/qtc_sort_paths.xml | 11 ++++ .../qtcreator/externaltools/qtc_toggle_if0.py | 38 +++++++++++ .../externaltools/qtc_toggle_if0.xml | 11 ++++ 10 files changed, 256 insertions(+) create mode 100755 utils_ide/qtcreator/externaltools/qtc_cpp_to_c_comments.py create mode 100644 utils_ide/qtcreator/externaltools/qtc_cpp_to_c_comments.xml create mode 100755 utils_ide/qtcreator/externaltools/qtc_expand_tabmix.py create mode 100644 utils_ide/qtcreator/externaltools/qtc_expand_tabmix.xml create mode 100755 utils_ide/qtcreator/externaltools/qtc_right_align_trailing_char.py create mode 100644 utils_ide/qtcreator/externaltools/qtc_right_align_trailing_char.xml create mode 100755 utils_ide/qtcreator/externaltools/qtc_sort_paths.py create mode 100644 utils_ide/qtcreator/externaltools/qtc_sort_paths.xml create mode 100755 utils_ide/qtcreator/externaltools/qtc_toggle_if0.py create mode 100644 utils_ide/qtcreator/externaltools/qtc_toggle_if0.xml diff --git a/utils_ide/qtcreator/externaltools/qtc_cpp_to_c_comments.py b/utils_ide/qtcreator/externaltools/qtc_cpp_to_c_comments.py new file mode 100755 index 0000000..dd11578 --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_cpp_to_c_comments.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +import sys + +txt = sys.stdin.read() + +data = txt.split("\n") + +# TODO. block comments + +# first detect blocks +def block_data(data, i_start): + ok = False + i_begin = -1 + i_index = -1 + i_end = -1 + i = i_start + while i < len(data): + l = data[i] + if "//" in l: + i_begin = i + i_index = l.index("//") + break + i += 1 + if i_begin != -1: + i_end = i_begin + for i in range(i_begin + 1, len(data)): + l = data[i] + if "//" in l and l.lstrip().startswith("//") and l.index("//") == i_index: + i_end = i + else: + break + + if i_begin != i_end: + # do a block comment replacement + data[i_begin] = data[i_begin].replace("//", "/*", 1) + for i in range(i_begin + 1, i_end + 1): + data[i] = data[i].replace("//", " *", 1) + data[i_end] = "%s */" % data[i_end].rstrip() + # done with block comment, still go onto do regular replace + return max(i_end, i_start + 1) + +i = 0 +while i < len(data): + i = block_data(data, i) + +i = 0 +while "//" not in data[i] and i > len(data): + i += 1 + + + + + +for i, l in enumerate(data): + if "//" in l: # should check if its in a string. + + text, comment = l.split("//", 1) + + l = "%s/* %s */" % (text, comment.strip()) + + data[i] = l + + +# list(map(print, data)) +print("\n".join(data), end="") diff --git a/utils_ide/qtcreator/externaltools/qtc_cpp_to_c_comments.xml b/utils_ide/qtcreator/externaltools/qtc_cpp_to_c_comments.xml new file mode 100644 index 0000000..4fc2b04 --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_cpp_to_c_comments.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<externaltool id="qtc_cpp_to_c_comments"> + <description>Convert blocks of C++ comments into C style comments.</description> + <displayname>C++ to C (Comments)</displayname> + <category>Formatting</category> + <executable output="replaceselection" error="showinpane" modifiesdocument="no"> + <path>qtc_cpp_to_c_comments.py</path> + <input>%{CurrentDocument:Selection}</input> + <workingdirectory>%{CurrentDocument:Path}</workingdirectory> + </executable> +</externaltool> diff --git a/utils_ide/qtcreator/externaltools/qtc_expand_tabmix.py b/utils_ide/qtcreator/externaltools/qtc_expand_tabmix.py new file mode 100755 index 0000000..aae1886 --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_expand_tabmix.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +import sys + +# TODO, get from QtCreator +TABSIZE = 4 + +txt = sys.stdin.read() +data = txt.split("\n") + +for i, l in enumerate(data): + l_lstrip = l.lstrip("\t") + l_lstrip_tot = (len(l) - len(l_lstrip)) + if l_lstrip_tot: + l_pre_ws, l_post_ws = l[:l_lstrip_tot], l[l_lstrip_tot:] + else: + l_pre_ws, l_post_ws = "", l + # expand tabs and remove trailing space + data[i] = l_pre_ws + l_post_ws.expandtabs(TABSIZE).rstrip(" \t") + + +print("\n".join(data), end="") diff --git a/utils_ide/qtcreator/externaltools/qtc_expand_tabmix.xml b/utils_ide/qtcreator/externaltools/qtc_expand_tabmix.xml new file mode 100644 index 0000000..4372ee3 --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_expand_tabmix.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<externaltool id="qtc_expand_tabmix"> + <description>Expand non-leading tabs into spaces.</description> + <displayname>Expand Tab Mix</displayname> + <category>Formatting</category> + <executable output="replaceselection" error="showinpane" modifiesdocument="no"> + <path>qtc_expand_tabmix.py</path> + <input>%{CurrentDocument:Selection}</input> + <workingdirectory>%{CurrentDocument:Path}</workingdirectory> + </executable> +</externaltool> diff --git a/utils_ide/qtcreator/externaltools/qtc_right_align_trailing_char.py b/utils_ide/qtcreator/externaltools/qtc_right_align_trailing_char.py new file mode 100755 index 0000000..7efc92c --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_right_align_trailing_char.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +import sys + +# TODO, get from QtCreator +TABSIZE = 4 + +txt = sys.stdin.read() +data = txt.split("\n") + +maxlen = 0 +# tabs -> spaces +for i, l in enumerate(data): + l = l.replace("\t", " " * TABSIZE) + l = l.rstrip() + maxlen = max(maxlen, len(l)) + data[i] = l + +for i, l in enumerate(data): + ws = l.rsplit(" ", 1) + if len(l.strip().split()) == 1 or len(ws) == 1: + pass + else: + j = 1 + while len(l) < maxlen: + l = (" " * j).join(ws) + j += 1 + data[i] = l + +# add tabs back in +for i, l in enumerate(data): + ls = l.lstrip() + d = len(l) - len(ls) + indent = "" + while d >= TABSIZE: + d -= TABSIZE + indent += "\t" + if d: + indent += (" " * d) + data[i] = indent + ls + + +print("\n".join(data), end="") diff --git a/utils_ide/qtcreator/externaltools/qtc_right_align_trailing_char.xml b/utils_ide/qtcreator/externaltools/qtc_right_align_trailing_char.xml new file mode 100644 index 0000000..c4ba429 --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_right_align_trailing_char.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<externaltool id="qtc_right_align_trailing_char"> + <description>Right align the last character of each line to the existing furthermost character (useful for multi-line macros).</description> + <displayname>Right Align Trailing Char</displayname> + <category>Formatting</category> + <executable output="replaceselection" error="showinpane" modifiesdocument="no"> + <path>qtc_right_align_trailing_char.py</path> + <input>%{CurrentDocument:Selection}</input> + <workingdirectory>%{CurrentDocument:Path}</workingdirectory> + </executable> +</externaltool> diff --git a/utils_ide/qtcreator/externaltools/qtc_sort_paths.py b/utils_ide/qtcreator/externaltools/qtc_sort_paths.py new file mode 100755 index 0000000..c584a4b --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_sort_paths.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +import sys + +txt = sys.stdin.read() + +data = txt.split("\n") + +class PathCMP: + def __init__(self, path): + path = path.strip() + + self.path = path + if path.startswith("."): + path = path[1:] + + if path.startswith("/"): + path = path[1:] + if path.endswith("/"): + path = path[:-1] + + self.level = self.path.count("..") + if self.level == 0: + self.level = (self.path.count("/") - 10000) + + def __eq__(self, other): + return self.path == other.path + def __lt__(self, other): + return self.path < other.path if self.level == other.level else self.level < other.level + def __gt__(self, other): + return self.path > other.path if self.level == other.level else self.level > other.level + + +data.sort(key=lambda a: PathCMP(a)) + +print("\n".join(data), end="") diff --git a/utils_ide/qtcreator/externaltools/qtc_sort_paths.xml b/utils_ide/qtcreator/externaltools/qtc_sort_paths.xml new file mode 100644 index 0000000..40c4073 --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_sort_paths.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<externaltool id="qtc_sort_paths"> + <description>Path sort selection, taking into account path depth.</description> + <displayname>Sort (Path Depths)</displayname> + <category>Formatting</category> + <executable output="replaceselection" error="showinpane" modifiesdocument="no"> + <path>qtc_sort_paths.py</path> + <input>%{CurrentDocument:Selection}</input> + <workingdirectory>%{CurrentDocument:Path}</workingdirectory> + </executable> +</externaltool> diff --git a/utils_ide/qtcreator/externaltools/qtc_toggle_if0.py b/utils_ide/qtcreator/externaltools/qtc_toggle_if0.py new file mode 100755 index 0000000..1942687 --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_toggle_if0.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +import sys + +txt = sys.stdin.read() +data = txt.split("\n") + +# Check if we're if0 +is_comment = False +for l in data: + l_strip = l.strip() + if l_strip: + if l_strip.startswith("#if 0"): + is_comment = True + else: + is_comment = False + break + +if is_comment: + pop_a = None + pop_b = None + for i, l in enumerate(data): + l_strip = l.strip() + + if pop_a is None: + if l_strip.startswith("#if 0"): + pop_a = i + + if l_strip.startswith("#endif"): + pop_b = i + + if pop_a is not None and pop_b is not None: + del data[pop_b] + del data[pop_a] +else: + data = ["\n#if 0"] + data + ["#endif\n"] + + +print("\n".join(data), end="") diff --git a/utils_ide/qtcreator/externaltools/qtc_toggle_if0.xml b/utils_ide/qtcreator/externaltools/qtc_toggle_if0.xml new file mode 100644 index 0000000..e63a18e --- /dev/null +++ b/utils_ide/qtcreator/externaltools/qtc_toggle_if0.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<externaltool id="qtc_toggle_if0"> + <description>Toggle if 0 preprocessor block.</description> + <displayname>Toggle #if 0</displayname> + <category>Formatting</category> + <executable output="replaceselection" error="showinpane" modifiesdocument="no"> + <path>qtc_toggle_if0.py</path> + <input>%{CurrentDocument:Selection}</input> + <workingdirectory>%{CurrentDocument:Path}</workingdirectory> + </executable> +</externaltool> -- GitLab