ycm-core / ycmd

A code-completion & code-comprehension server
https://ycm-core.github.io/ycmd/
GNU General Public License v3.0
1.69k stars 765 forks source link

TS: include `@param` and `@returns` in docs #1675

Closed notEvil closed 8 months ago

notEvil commented 1 year ago

Hi,

would you consider adding @param and @returns for JavaScript/TypeScript docs? Like

--- a/ycmd/completers/typescript/typescript_completer.py
+++ b/ycmd/completers/typescript/typescript_completer.py
@@ -1128,6 +1128,27 @@ def _BuildCompletionExtraMenuAndDetailedInfo( request_data, entry ):
     extra_menu_info = re.sub( '\\s+', ' ', signature )
     detailed_info = [ signature ]

+  interface = []
+  returns = None
+
+  for tag in entry.get("tags", []):
+    if tag["name"] == "param":
+      text = tag["text"]
+      match = re.search("\s", text)
+      index = len(text) if match is None else match.start(0)
+      interface.append((text[:index], text[index + 1 :].strip()))
+
+    elif tag["name"] == "returns":
+      returns = tag["text"]
+
+  if returns is not None:
+    interface.append(("return", returns))
+
+  if len(interface) != 0:
+    length = max(len(name) for name, _ in interface)
+    _ = '\n'.join(f'{name.rjust(length)}: {text}' for name, text in interface)
+    detailed_info.append(_)
+
   docs = entry.get( 'documentation', [] )
   detailed_info += [ doc[ 'text' ].strip() for doc in docs if doc ]
   detailed_info = '\n\n'.join( detailed_info )

https://github.com/ycm-core/ycmd/blob/f53e7acf511748dd50f733c11b7de93416410c17/ycmd/completers/typescript/typescript_completer.py#L1130

The format (e.g. .rjust and ": ") is obviously a matter of taste.

puremourning commented 1 year ago

sure, I don't see why not. Happy to review a PR with tests.

Would also like to see some example screenshots in YCM of GetDoc and the equivalent Hover, just to make sure it looks sane, but I have no objection to a contribution in this area.