Accept docstrings with last line not fully indented

When trimming indent, the last line, if only containing
whitespaces does not need to match the indent, so that
this indentation style works:

    -docstring %{
        indented string
    }
This commit is contained in:
Maxime Coste 2019-09-17 21:48:00 +10:00
parent c787128a7c
commit 8cca77c949
2 changed files with 21 additions and 25 deletions

View File

@ -26,10 +26,11 @@ define-command -params ..1 \
fi
cat "$namecache"
done} \
-docstring %{ctags-search [<symbol>]: jump to a symbol's definition
If no symbol is passed then the current selection is used as symbol name} \
ctags-search \
%[ evaluate-commands %sh[
-docstring %{
ctags-search [<symbol>]: jump to a symbol's definition
If no symbol is passed then the current selection is used as symbol name
} \
ctags-search %[ evaluate-commands %sh[
realpath() { ( cd "$(dirname "$1")"; printf "%s/%s\n" "$(pwd -P)" "$(basename "$1")" ) }
export tagname="${1:-${kak_selection}}"
eval "set -- $kak_quoted_opt_ctagsfiles"

View File

@ -23,31 +23,25 @@ String trim_indent(StringView str)
{
if (str.empty())
return {};
else if (str[0_byte] != '\n')
return trim_whitespaces(str).str();
if (str[0_byte] == '\n')
str = str.substr(1_byte);
const CharCount docstring_length = str.char_length();
while (not str.empty() and is_blank(str.back()))
str = str.substr(0, str.length() - 1);
CharCount level_indent = 0;
while (level_indent < docstring_length
and is_horizontal_blank(str[level_indent]))
level_indent++;
utf8::iterator it{str.begin(), str};
while (it != str.end() and is_horizontal_blank(*it))
it++;
if (level_indent >= docstring_length or not level_indent)
return trim_whitespaces(str).str();
const auto str_indent = str.substr(0, level_indent);
auto s = str | split<StringView>('\n') | transform([&](auto&& line) {
if (line.empty())
const StringView indent{str.begin(), it.base()};
return accumulate(str | split_after<StringView>('\n') | transform([&](auto&& line) {
if (line == "\n")
return line;
else if (not prefix_match(line, str_indent))
else if (not prefix_match(line, indent))
throw runtime_error("inconsistent indentation in the string");
return line.substr(str_indent.char_length());
});
return trim_whitespaces(join(s, '\n', false)).str();
return line.substr(indent.length());
}), String{}, [](String& s, StringView l) -> decltype(auto) { return s += l; });
}
String escape(StringView str, StringView characters, char escape)
@ -414,6 +408,7 @@ UnitTest test_string{[]()
kak_assert(trim_indent("\nno-indent") == "no-indent");
kak_assert(trim_indent("\n indent\n indent") == "indent\nindent");
kak_assert(trim_indent("\n indent\n indent") == "indent\n indent");
kak_assert(trim_indent("\n indent\n indent\n ") == "indent\nindent");
kak_expect_throw(runtime_error, trim_indent("\n indent\nno-indent"));