Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hover show detail feature + minor bug fix #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/vscode-atopile/bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,58 @@ def completions(params: Optional[lsp.CompletionParams] = None) -> lsp.Completion
return lsp.CompletionList(is_incomplete=False, items=items,)


@LSP_SERVER.feature(lsp.TEXT_DOCUMENT_HOVER)
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that utils.remove_special_character is defined and correctly removes or handles special characters from the word to avoid potential errors or unexpected behavior.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new hover_definition function is a significant enhancement for the user experience, providing detailed information on hover. It's important to ensure that the performance impact is minimal, especially for large files. Consider adding performance benchmarks or optimizing the retrieval of hover information if necessary.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure utils.remove_special_character method is implemented in lsp_utils.py as it's critical for the hover feature's functionality and the associated bug fix.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider handling exceptions more specifically to provide clearer error messages or fallbacks, especially for KeyError, AtoError, and AttributeError within the hover feature implementation.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new hover feature is a significant enhancement for the user experience, providing quick insights into class names and variable values directly in the editor. It's important to ensure that the hover information is accurate and performs well, even in large files or complex projects. Consider adding tests to cover various scenarios, including edge cases where instances or variables might not follow conventional naming or structuring.

def hover_definition(params: lsp.HoverParams) -> Optional[lsp.Hover]:
if not params.text_document.uri.startswith("file://"):
return lsp.CompletionList(is_incomplete=False, items=[])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a CompletionList for a hover request is incorrect. It should return None instead.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning lsp.CompletionList in hover_definition is incorrect. It should return None instead.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning lsp.CompletionList in a hover handler is incorrect. It should return None instead.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning an empty completion list for hover requests is not appropriate. Consider returning None instead.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning lsp.CompletionList for hover feature is incorrect. It should return None instead.


document = LSP_SERVER.workspace.get_text_document(params.text_document.uri)

file = Path(document.path)
try:
atopile.config.get_project_context()
except ValueError:
atopile.config.set_project_context(atopile.config.ProjectContext.from_path(file))
class_addr = _get_def_addr_from_line(file, params.position.line)
if not class_addr:
class_addr = str(file)

word, range_ = utils.cursor_word_and_range(document, params.position)
try:
word = word[:word.index(".", params.position.character - range_.start.character)]
except ValueError:
pass
word = utils.remove_special_character(word)
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of utils.remove_special_character(word) is a good addition for handling the minor bug related to brackets in words. Ensure that this utility function is robust and handles all edge cases that might be encountered in different programming languages or file types.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition of utils.remove_special_character(word) is a thoughtful approach to handling edge cases like brackets in words. It's a good practice to document the specific cases this bug fix addresses, ensuring future maintainers understand the context and reasoning behind this change.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function remove_special_character is not defined in the provided context. Ensure it is imported or defined.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remove_special_character function is not defined in the provided context. Ensure this function is implemented or imported correctly.

output_str = ""

# check if it is an instance
try:
instance_addr = atopile.address.add_instances(class_addr, word.split("."))
instance = atopile.front_end.lofty.get_instance(instance_addr)
except (KeyError, atopile.errors.AtoError, AttributeError):
pass
else:
# TODO: deal with assignments made to super
output_str += f"**class**: {str(atopile.address.get_name(instance.supers[0].address))}\n\n"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider handling cases where instance.supers might be empty.

for key, assignment in instance.assignments.items():
output_str += "**"+key+"**: "
if (assignment[0] is None):
output_str += 'not assigned\n\n'
else:
output_str += str(assignment[0].value) +'\n\n'
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +228 to +235
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach to display class names and variable values is clear and concise. However, consider handling edge cases where instance.supers[0].address might not exist or assignment[0] is None. Adding error handling or checks could prevent potential runtime errors.

Comment on lines +228 to +235

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the Markdown formatting for hover content does not introduce formatting issues in the LSP client. Specifically, verify that newline and bold formatting are displayed as expected.

Comment on lines +228 to +235
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handling of instance assignments and the display of their values is well-implemented. However, consider the scalability of this approach for instances with a large number of assignments or deeply nested structures. It might be beneficial to limit the depth or quantity of information displayed to keep the hover popup manageable and readable.


# check if it is an assignment
class_assignments = atopile.front_end.dizzy.get_layer(class_addr).assignments.get(word, None)
if class_assignments:
output_str = str(class_assignments.value)
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +238 to +240
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retrieving class assignments using atopile.front_end.dizzy.get_layer(class_addr).assignments.get(word, None) is a neat way to fetch assigned values. Ensure that dizzy.get_layer and the assignments dictionary are always up-to-date with the latest changes in the document to maintain accuracy in hover information.

Comment on lines +238 to +240

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validate that class_assignments.value is always in a format suitable for direct string conversion and presentation in the hover content.

Comment on lines +238 to +240
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retrieving and displaying the value of class assignments is a useful feature. Ensure that the value's string representation is user-friendly and informative. For complex objects, consider how much detail to show, as overly verbose information might overwhelm the user.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assigning to output_str directly here will overwrite any instance details. Consider appending instead.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output_str should be appended to rather than replaced when handling class assignments to avoid overwriting instance details.


if output_str:
return lsp.Hover(contents=lsp.MarkupContent(
kind=lsp.MarkupKind.Markdown,
value=output_str.strip(),
))
return None

@LSP_SERVER.feature(lsp.TEXT_DOCUMENT_DEFINITION)
def goto_definition(params: Optional[lsp.DefinitionParams] = None) -> Optional[lsp.Location]:
"""Handler for goto definition."""
Expand All @@ -207,7 +259,6 @@ def goto_definition(params: Optional[lsp.DefinitionParams] = None) -> Optional[l
atopile.config.get_project_context()
except ValueError:
atopile.config.set_project_context(atopile.config.ProjectContext.from_path(file))

class_addr = _get_def_addr_from_line(file, params.position.line)
if not class_addr:
class_addr = str(file)
Expand All @@ -217,9 +268,11 @@ def goto_definition(params: Optional[lsp.DefinitionParams] = None) -> Optional[l
word = word[:word.index(".", params.position.character - range_.start.character)]
except ValueError:
pass
word = utils.remove_special_character(word)

# See if it's an instance
instance_addr = atopile.address.add_instances(class_addr, word.split("."))

src_ctx = None
try:
src_ctx = atopile.front_end.lofty.get_instance(instance_addr).src_ctx
Expand Down
3 changes: 3 additions & 0 deletions src/vscode-atopile/bundled/tool/lsp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def cursor_word_and_range(
return word
return None

def remove_special_character(word: str) -> str:
return "".join(e for e in word if e not in "(){}")
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +71 to +72

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a brief docstring to remove_special_character function to explain its purpose and usage, especially how it relates to the hover feature and the handling of brackets in words.

Comment on lines +71 to +72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new function remove_special_character is a useful addition for sanitizing words by removing specific special characters. It's a good practice to explicitly list which characters are considered special in the function's documentation for clarity and maintainability. Also, consider edge cases where removing these characters might alter the intended meaning or functionality of the word in different contexts.

Comment on lines +71 to +72

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extending the set of characters to remove based on potential future requirements or ensuring this set covers all cases expected by the hover feature.

Comment on lines +71 to +72

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a brief docstring to remove_special_character function to explain its purpose, especially the context in which it should be used within the LSP utilities.

Comment on lines +71 to +72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new function remove_special_character effectively strips parentheses and curly braces from the input string. While this is generally useful, consider if there are other special characters that might also need to be removed in the context of your LSP application. Additionally, it might be beneficial to document the specific use case for this function within the codebase to clarify its intended usage and help future contributors understand its role.


Comment on lines +71 to +73

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a docstring to explain the purpose and usage of this function.

def as_list(content: Union[Any, List[Any], Tuple[Any]]) -> Union[List[Any], Tuple[Any]]:
"""Ensures we always get a list"""
if isinstance(content, (list, tuple)):
Expand Down
3 changes: 2 additions & 1 deletion src/vscode-atopile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"virtualWorkspaces": {
"supported": false,
"description": "Virtual Workspaces are not supported with atopile."
}
},
"hoverProvider" : "true"
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
vkameswaran marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +42 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the value for hoverProvider is a boolean (true) instead of a string to correctly enable hover support according to the VS Code extension API.

Comment on lines +42 to +43
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding 'hoverProvider': true is a great feature enhancement. It's important to ensure that the corresponding implementation is robust and efficiently handles edge cases, especially for complex atopile codebases. Consider adding documentation or tooltips in the UI to inform users about this new feature and how they can make the most of it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value for 'hoverProvider' should be a boolean true (without quotes) instead of a string 'true'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value for hoverProvider should be a boolean true instead of a string. Replace "true" with true.

Comment on lines +42 to +43
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding 'hoverProvider' with a value of 'true' enables hover support, which is a great feature for improving developer experience. However, the value should ideally be a boolean true rather than a string "true". Please verify if this implementation aligns with the expected format for VS Code extensions.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value for hoverProvider should be a boolean (true) instead of a string ("true").

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value for hoverProvider should be a boolean (true) instead of a string ("true"). This ensures proper interpretation by VS Code.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value for hoverProvider should be a boolean. Change "true" to true.

},
"activationEvents": [
"onLanguage:atopile",
Expand Down