Format documentations to markdown only when needed

This commit is contained in:
geequlim 2019-10-05 14:07:10 +08:00
parent e199488bc7
commit 9f27a4838c
4 changed files with 82 additions and 74 deletions

View file

@ -157,7 +157,7 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
r_symbol.selectionRange.start.line = r_symbol.range.start.line; r_symbol.selectionRange.start.line = r_symbol.range.start.line;
r_symbol.detail = "class " + r_symbol.name; r_symbol.detail = "class " + r_symbol.name;
bool is_root_class = &r_symbol == &class_symbol; bool is_root_class = &r_symbol == &class_symbol;
r_symbol.documentation = parse_documentation_as_markdown(is_root_class ? 0 : LINE_NUMBER_TO_INDEX(p_class->line), is_root_class); r_symbol.documentation = parse_documentation(is_root_class ? 0 : LINE_NUMBER_TO_INDEX(p_class->line), is_root_class);
for (int i = 0; i < p_class->variables.size(); ++i) { for (int i = 0; i < p_class->variables.size(); ++i) {
@ -184,7 +184,7 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
symbol.detail += " = " + JSON::print(m.default_value); symbol.detail += " = " + JSON::print(m.default_value);
} }
symbol.documentation = parse_documentation_as_markdown(line); symbol.documentation = parse_documentation(line);
symbol.uri = uri; symbol.uri = uri;
symbol.script_path = path; symbol.script_path = path;
@ -204,7 +204,7 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
symbol.range.end.line = symbol.range.start.line; symbol.range.end.line = symbol.range.start.line;
symbol.range.end.character = lines[line].length(); symbol.range.end.character = lines[line].length();
symbol.selectionRange.start.line = symbol.range.start.line; symbol.selectionRange.start.line = symbol.range.start.line;
symbol.documentation = parse_documentation_as_markdown(line); symbol.documentation = parse_documentation(line);
symbol.uri = uri; symbol.uri = uri;
symbol.script_path = path; symbol.script_path = path;
symbol.detail = "signal " + signal.name + "("; symbol.detail = "signal " + signal.name + "(";
@ -233,7 +233,7 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
symbol.range.end.line = symbol.range.start.line; symbol.range.end.line = symbol.range.start.line;
symbol.range.end.character = lines[line].length(); symbol.range.end.character = lines[line].length();
symbol.selectionRange.start.line = symbol.range.start.line; symbol.selectionRange.start.line = symbol.range.start.line;
symbol.documentation = parse_documentation_as_markdown(line); symbol.documentation = parse_documentation(line);
symbol.uri = uri; symbol.uri = uri;
symbol.script_path = path; symbol.script_path = path;
@ -301,7 +301,7 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN
r_symbol.range.end.line = MAX(p_func->body->end_line - 2, p_func->body->line); r_symbol.range.end.line = MAX(p_func->body->end_line - 2, p_func->body->line);
r_symbol.range.end.character = lines[r_symbol.range.end.line].length(); r_symbol.range.end.character = lines[r_symbol.range.end.line].length();
r_symbol.selectionRange.start.line = r_symbol.range.start.line; r_symbol.selectionRange.start.line = r_symbol.range.start.line;
r_symbol.documentation = parse_documentation_as_markdown(line); r_symbol.documentation = parse_documentation(line);
r_symbol.uri = uri; r_symbol.uri = uri;
r_symbol.script_path = path; r_symbol.script_path = path;
@ -359,65 +359,11 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN
if (var->datatype.kind != GDScriptParser::DataType::UNRESOLVED) { if (var->datatype.kind != GDScriptParser::DataType::UNRESOLVED) {
symbol.detail += ": " + var->datatype.to_string(); symbol.detail += ": " + var->datatype.to_string();
} }
symbol.documentation = parse_documentation_as_markdown(line); symbol.documentation = parse_documentation(line);
r_symbol.children.push_back(symbol); r_symbol.children.push_back(symbol);
} }
} }
String ExtendGDScriptParser::marked_documentation(const String &p_bbcode) {
String markdown = p_bbcode.strip_edges();
Vector<String> lines = markdown.split("\n");
bool in_code_block = false;
int code_block_indent = -1;
markdown = "";
for (int i = 0; i < lines.size(); i++) {
String line = lines[i];
int block_start = line.find("[codeblock]");
if (block_start != -1) {
code_block_indent = block_start;
in_code_block = true;
line = "\n";
} else if (in_code_block) {
line = "\t" + line.substr(code_block_indent, line.length());
}
if (in_code_block && line.find("[/codeblock]") != -1) {
line = "\n";
in_code_block = false;
}
if (!in_code_block) {
line = line.strip_edges();
line = line.replace("[code]", "`");
line = line.replace("[/code]", "`");
line = line.replace("[i]", "*");
line = line.replace("[/i]", "*");
line = line.replace("[b]", "**");
line = line.replace("[/b]", "**");
line = line.replace("[u]", "__");
line = line.replace("[/u]", "__");
line = line.replace("[method ", "`");
line = line.replace("[member ", "`");
line = line.replace("[signal ", "`");
line = line.replace("[enum ", "`");
line = line.replace("[constant ", "`");
line = line.replace("[", "`");
line = line.replace("]", "`");
}
if (!in_code_block && i < lines.size() - 1) {
line += "\n\n";
} else if (i < lines.size() - 1) {
line += "\n";
}
markdown += line;
}
return markdown;
}
String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) { String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) {
ERR_FAIL_INDEX_V(p_line, lines.size(), String()); ERR_FAIL_INDEX_V(p_line, lines.size(), String());
@ -576,10 +522,6 @@ const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(i
return ret; return ret;
} }
String ExtendGDScriptParser::parse_documentation_as_markdown(int p_line, bool p_docs_down) {
return marked_documentation(parse_documentation(p_line, p_docs_down));
}
const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const { const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const {
if (p_line <= 0) { if (p_line <= 0) {
return &class_symbol; return &class_symbol;

View file

@ -75,11 +75,6 @@ class ExtendGDScriptParser : public GDScriptParser {
Array member_completions; Array member_completions;
String parse_documentation_as_markdown(int p_line, bool p_docs_down = false);
public:
static String marked_documentation(const String &p_bbcode);
public: public:
_FORCE_INLINE_ const String &get_path() const { return path; } _FORCE_INLINE_ const String &get_path() const { return path; }
_FORCE_INLINE_ const Vector<String> &get_lines() const { return lines; } _FORCE_INLINE_ const Vector<String> &get_lines() const { return lines; }

View file

@ -198,7 +198,7 @@ Error GDScriptWorkspace::initialize() {
if (!class_data.inherits.empty()) { if (!class_data.inherits.empty()) {
class_symbol.detail += " extends " + class_data.inherits; class_symbol.detail += " extends " + class_data.inherits;
} }
class_symbol.documentation = ExtendGDScriptParser::marked_documentation(class_data.brief_description) + "\n" + ExtendGDScriptParser::marked_documentation(class_data.description); class_symbol.documentation = class_data.brief_description + "\n" + class_data.description;
for (int i = 0; i < class_data.constants.size(); i++) { for (int i = 0; i < class_data.constants.size(); i++) {
const DocData::ConstantDoc &const_data = class_data.constants[i]; const DocData::ConstantDoc &const_data = class_data.constants[i];
@ -211,7 +211,7 @@ Error GDScriptWorkspace::initialize() {
symbol.detail += ": " + const_data.enumeration; symbol.detail += ": " + const_data.enumeration;
} }
symbol.detail += " = " + const_data.value; symbol.detail += " = " + const_data.value;
symbol.documentation = ExtendGDScriptParser::marked_documentation(const_data.description); symbol.documentation = const_data.description;
class_symbol.children.push_back(symbol); class_symbol.children.push_back(symbol);
} }
@ -232,7 +232,7 @@ Error GDScriptWorkspace::initialize() {
} else { } else {
symbol.detail += ": " + data.type; symbol.detail += ": " + data.type;
} }
symbol.documentation = ExtendGDScriptParser::marked_documentation(data.description); symbol.documentation = data.description;
class_symbol.children.push_back(symbol); class_symbol.children.push_back(symbol);
} }
@ -270,7 +270,7 @@ Error GDScriptWorkspace::initialize() {
} }
symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + data.return_type; symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + data.return_type;
symbol.documentation = ExtendGDScriptParser::marked_documentation(data.description); symbol.documentation = data.description;
class_symbol.children.push_back(symbol); class_symbol.children.push_back(symbol);
} }

View file

@ -37,6 +37,9 @@ namespace lsp {
typedef String DocumentUri; typedef String DocumentUri;
/** Format BBCode documentation from DocData to markdown */
static String marked_documentation(const String &p_bbcode);
/** /**
* Text documents are identified using a URI. On the protocol level, URIs are passed as strings. * Text documents are identified using a URI. On the protocol level, URIs are passed as strings.
*/ */
@ -282,6 +285,9 @@ struct Command {
} }
}; };
// Use namespace instead of enumeration to follow the LSP specifications
// lsp::EnumName::EnumValue is OK but lsp::EnumValue is not
namespace TextDocumentSyncKind { namespace TextDocumentSyncKind {
/** /**
* Documents should not be synced at all. * Documents should not be synced at all.
@ -592,6 +598,7 @@ struct TextDocumentContentChangeEvent {
} }
}; };
// Use namespace instead of enumeration to follow the LSP specifications
namespace DiagnosticSeverity { namespace DiagnosticSeverity {
/** /**
* Reports an error. * Reports an error.
@ -692,6 +699,7 @@ struct Diagnostic {
} }
}; };
// Use namespace instead of enumeration to follow the LSP specifications
/** /**
* Describes the content type that a client supports in various * Describes the content type that a client supports in various
* result literals like `Hover`, `ParameterInfo` or `CompletionItem`. * result literals like `Hover`, `ParameterInfo` or `CompletionItem`.
@ -756,6 +764,9 @@ struct MarkupContent {
} }
}; };
// Use namespace instead of enumeration to follow the LSP specifications
// lsp::EnumName::EnumValue is OK but lsp::EnumValue is not
// And here C++ compilers are unhappy with our enumeration name like Color, File, Reference etc.
/** /**
* The kind of a completion entry. * The kind of a completion entry.
*/ */
@ -787,6 +798,7 @@ static const int Operator = 24;
static const int TypeParameter = 25; static const int TypeParameter = 25;
}; // namespace CompletionItemKind }; // namespace CompletionItemKind
// Use namespace instead of enumeration to follow the LSP specifications
/** /**
* Defines whether the insert text in a completion item should be interpreted as * Defines whether the insert text in a completion item should be interpreted as
* plain text or a snippet. * plain text or a snippet.
@ -979,6 +991,9 @@ struct CompletionList {
Vector<CompletionItem> items; Vector<CompletionItem> items;
}; };
// Use namespace instead of enumeration to follow the LSP specifications
// lsp::EnumName::EnumValue is OK but lsp::EnumValue is not
// And here C++ compilers are unhappy with our enumeration name like String, Array, Object etc
/** /**
* A symbol kind. * A symbol kind.
*/ */
@ -1181,7 +1196,7 @@ struct DocumentSymbol {
markdown.value = "\t" + detail + "\n\n"; markdown.value = "\t" + detail + "\n\n";
} }
if (documentation.length()) { if (documentation.length()) {
markdown.value += documentation + "\n\n"; markdown.value += marked_documentation(documentation) + "\n\n";
} }
if (script_path.length()) { if (script_path.length()) {
markdown.value += "Defined in [" + script_path + "](" + uri + ")"; markdown.value += "Defined in [" + script_path + "](" + uri + ")";
@ -1304,6 +1319,7 @@ struct FoldingRange {
} }
}; };
// Use namespace instead of enumeration to follow the LSP specifications
/** /**
* How a completion was triggered * How a completion was triggered
*/ */
@ -1551,6 +1567,61 @@ struct InitializeResult {
} }
}; };
/** Format BBCode documentation from DocData to markdown */
static String marked_documentation(const String &p_bbcode) {
String markdown = p_bbcode.strip_edges();
Vector<String> lines = markdown.split("\n");
bool in_code_block = false;
int code_block_indent = -1;
markdown = "";
for (int i = 0; i < lines.size(); i++) {
String line = lines[i];
int block_start = line.find("[codeblock]");
if (block_start != -1) {
code_block_indent = block_start;
in_code_block = true;
line = "\n";
} else if (in_code_block) {
line = "\t" + line.substr(code_block_indent, line.length());
}
if (in_code_block && line.find("[/codeblock]") != -1) {
line = "\n";
in_code_block = false;
}
if (!in_code_block) {
line = line.strip_edges();
line = line.replace("[code]", "`");
line = line.replace("[/code]", "`");
line = line.replace("[i]", "*");
line = line.replace("[/i]", "*");
line = line.replace("[b]", "**");
line = line.replace("[/b]", "**");
line = line.replace("[u]", "__");
line = line.replace("[/u]", "__");
line = line.replace("[method ", "`");
line = line.replace("[member ", "`");
line = line.replace("[signal ", "`");
line = line.replace("[enum ", "`");
line = line.replace("[constant ", "`");
line = line.replace("[", "`");
line = line.replace("]", "`");
}
if (!in_code_block && i < lines.size() - 1) {
line += "\n\n";
} else if (i < lines.size() - 1) {
line += "\n";
}
markdown += line;
}
return markdown;
}
} // namespace lsp } // namespace lsp
#endif #endif