Skip to content
Snippets Groups Projects
Commit cde6b6a5 authored by Campbell Barton's avatar Campbell Barton
Browse files

add other utilities

parent e3403649
Branches
No related tags found
No related merge requests found
Showing with 256 additions and 0 deletions
#!/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="")
<?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>
#!/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="")
<?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>
#!/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="")
<?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>
#!/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="")
<?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>
#!/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="")
<?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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment