[prev in list] [next in list] [prev in thread] [next in thread]
List: kwrite-devel
Subject: Re: Text wrapping Python docstrings
From: Yannick Gingras <ygingras () ygingras ! net>
Date: 2022-02-02 15:06:43
Message-ID: 380cbe6ae032aabc0b106c51bab1b8e9274c7790.camel () ygingras ! net
[Download RAW message or body]
[Attachment #2 (multipart/mixed)]
I got it to work exactly the way I want with the External Tools plugin.
This plugin makes is really easy to bind the script to a keyboard
shortcut. For reference, I'm attaching the quick and dirty script I use
to re-format the text.
On Tue, 2022-02-01 at 22:35 -0800, Yannick Gingras wrote:
> On Wed, 2022-02-02 at 11:11 +0500, Waqar Ahmed wrote:
> > I don't think there is an easy way atm, but maybe you can use the
> > "script" api to write a script that adjust the indent for you?
>
> My javascript is not really good, but it would be really easy for me
> to
> do a Python command line and to pass the fragment to "filter through
> command". Is there a way I could bind that to a keyboard shortcut?
> Let's say rebind ALT-W to "filter throught re-fill.py" without
> opening
> the command prompt?
>
--
Yannick Gingras
http://ygingras.net
["refill.py" (refill.py)]
#!/bin/env python3
""" Re-indent the text passed on sys.stdin as a paragrath at most MAX_WIDTH
wide, perserving the initial indentation. """
import re
import sys
LOGFILE = "/tmp/refill.log"
MAX_WIDTH = 79
INDENT_PAT = r"^(\s*)"
def reindent(text):
indent = re.match(INDENT_PAT, text).group()
words = text.split()
lines = []
cur_line = [indent]
cur_length = len(indent)
for word in words:
if cur_length + len(word) > MAX_WIDTH:
lines.append("".join(cur_line))
cur_line = [indent, word, " "]
cur_length = sum(map(len, cur_line))
else:
cur_line += [word, " "]
cur_length += len(word) + 1
if cur_line:
if cur_line[-1] == " ":
cur_line.pop()
lines.append("".join(cur_line))
return "\n".join(lines)
def main():
data = sys.stdin.read()
print(reindent(data))
if __name__ == "__main__":
main()
["signature.asc" (application/pgp-signature)]
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic