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

Version to test #416

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ An example for a Configuration file is given below
"incremental_sync": true,
"lowercase_intrinsics": true,
"hover_signature": true,
"folding_range": true,
"use_signature_help": true,
"excl_paths": ["tests/**", "tools/**"],
"excl_suffixes": ["_skip.f90"],
Expand Down
2 changes: 2 additions & 0 deletions docs/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ All the ``fortls`` settings with their default arguments can be found below
"hover_signature": false,
"hover_language": "fortran90",

"folding_range": false

"max_line_length": -1,
"max_comment_line_length": -1,
"disable_diagnostics": false,
Expand Down
6 changes: 6 additions & 0 deletions fortls/fortls.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@
"title": "Hover Language",
"type": "string"
},
"folding_range": {
"default": false,
"description": "Fold editor based on language keywords",
"title": "Folding Range",
"type": "boolean"
},
"max_line_length": {
"default": -1,
"description": "Maximum line length (default: -1)",
Expand Down
7 changes: 7 additions & 0 deletions fortls/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ def cli(name: str = "fortls") -> argparse.ArgumentParser:
),
)

# Folding range ------------------------------------------------------------
group.add_argument(
"--folding_range",
action="store_true",
help="Fold editor based on language keywords",
)

# Diagnostic options -------------------------------------------------------
group = parser.add_argument_group("Diagnostic options (error swigles)")
group.add_argument(
Expand Down
55 changes: 55 additions & 0 deletions fortls/langserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def noop(request: dict):
"textDocument/hover": self.serve_hover,
"textDocument/implementation": self.serve_implementation,
"textDocument/rename": self.serve_rename,
"textDocument/foldingRange": self.serve_folding_range,
"textDocument/didOpen": self.serve_onOpen,
"textDocument/didSave": self.serve_onSave,
"textDocument/didClose": self.serve_onClose,
Expand Down Expand Up @@ -226,6 +227,7 @@ def serve_initialize(self, request: dict):
"renameProvider": True,
"workspaceSymbolProvider": True,
"textDocumentSync": self.sync_type,
"foldingRangeProvider": True,
}
if self.use_signature_help:
server_capabilities["signatureHelpProvider"] = {
Expand Down Expand Up @@ -1224,6 +1226,56 @@ def serve_rename(self, request: dict):
)
return {"changes": changes}

def serve_folding_range(self, request: dict):
# Get parameters from request
params: dict = request["params"]
uri: str = params["textDocument"]["uri"]
path = path_from_uri(uri)
# Find object
file_obj = self.workspace.get(path)
if file_obj is None:
return None
if file_obj.ast is None:
return None
else:
folding_start = file_obj.ast.folding_start
folding_end = file_obj.ast.folding_end
if (
folding_start is None
or folding_end is None
or len(folding_start) != len(folding_end)
):
return None
# Construct folding_rage list
folding_ranges = []
# First treating scope objects...
for scope in file_obj.ast.scope_list:
n_mlines = len(scope.mlines)
# ...with intermediate folding lines (if, select)...
if n_mlines > 0:
self.add_range(folding_ranges, scope.sline - 1, scope.mlines[0] - 2)
for i in range(1, n_mlines):
self.add_range(
folding_ranges, scope.mlines[i - 1] - 1, scope.mlines[i] - 2
)
self.add_range(folding_ranges, scope.mlines[-1] - 1, scope.eline - 2)
# ...and without
else:
self.add_range(folding_ranges, scope.sline - 1, scope.eline - 2)
# Then treat comment blocks
folds = len(folding_start)
for i in range(0, folds):
self.add_range(folding_ranges, folding_start[i] - 1, folding_end[i] - 1)

return folding_ranges

def add_range(self, folding_ranges: list, start: int, end: int):
folding_range = {
"startLine": start,
"endLine": end,
}
folding_ranges.append(folding_range)

def serve_codeActions(self, request: dict):
params: dict = request["params"]
uri: str = params["textDocument"]["uri"]
Expand Down Expand Up @@ -1621,6 +1673,9 @@ def _load_config_file_general(self, config_dict: dict) -> None:
self.hover_signature = config_dict.get("hover_signature", self.hover_signature)
self.hover_language = config_dict.get("hover_language", self.hover_language)

# Folding range --------------------------------------------------------
self.folding_range = config_dict.get("folding_range", self.folding_range)

# Diagnostic options ---------------------------------------------------
self.max_line_length = config_dict.get("max_line_length", self.max_line_length)
self.max_comment_line_length = config_dict.get(
Expand Down
4 changes: 4 additions & 0 deletions fortls/parsers/internal/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def __init__(self, file_obj=None):
self.inherit_objs: list = []
self.linkable_objs: list = []
self.external_objs: list = []
self.folding_start: list = []
self.folding_end: list = []
self.comment_block_start = 0
self.comment_block_end = 0
self.none_scope = None
self.inc_scope = None
self.current_scope = None
Expand Down
Loading
Loading