Skip to content

Commit c97331a

Browse files
committed
update locking mechanism
1 parent 1879d9c commit c97331a

File tree

18 files changed

+471
-340
lines changed

18 files changed

+471
-340
lines changed

robotcode/analyzer/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
async def main() -> None:
21-
async for f in iter_files(
21+
for f in iter_files(
2222
Path("c:/develop/robot/robotframework"),
2323
f"**/*.{{{ROBOT_FILE_EXTENSION[1:]},{RESOURCE_FILE_EXTENSION[1:]}}}",
2424
ignore_patterns=exclude_patterns,

robotcode/language_server/common/parts/diagnostics.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,16 @@ async def on_get_diagnostics_mode(sender, uri: Uri) -> Optional[DiagnosticsMode]
216216
...
217217

218218
async def ensure_workspace_loaded(self) -> None:
219-
if not self._workspace_loaded:
220-
async with self._workspace_load_lock:
221-
if not self._workspace_loaded:
222-
if self.workspace_loaded_event.is_set():
223-
return
224-
225-
try:
226-
await self.load_workspace_documents(self)
227-
finally:
228-
self._workspace_loaded = True
229-
self.workspace_loaded_event.set()
230-
await self.on_workspace_loaded(self)
231-
await self.force_refresh_all()
219+
async with self._workspace_load_lock:
220+
if not self._workspace_loaded and not self.workspace_loaded_event.is_set():
221+
self._logger.debug("load workspace documents")
222+
try:
223+
await self.load_workspace_documents(self)
224+
finally:
225+
self._workspace_loaded = True
226+
self.workspace_loaded_event.set()
227+
await self.on_workspace_loaded(self)
228+
await self.force_refresh_all()
232229

233230
async def force_refresh_all(self, refresh: bool = True) -> None:
234231
for doc in self.parent.documents.documents:
@@ -293,9 +290,12 @@ async def run_workspace_diagnostics(self) -> None:
293290
documents = [
294291
doc
295292
for doc in self.parent.documents.documents
296-
if (data := self.get_diagnostics_data(doc)).force
297-
or doc.version != data.version
298-
or data.task is None
293+
if not doc.opened_in_editor
294+
and (
295+
(data := self.get_diagnostics_data(doc)).force
296+
or doc.version != data.version
297+
or data.task is None
298+
)
299299
]
300300

301301
if len(documents) == 0:

robotcode/language_server/common/parts/documents.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ async def _text_document_did_open(self, text_document: TextDocumentItem, *args:
258258

259259
self._documents[uri] = document
260260
else:
261-
text_changed = document.text != normalized_text
261+
text_changed = await document.text() != normalized_text
262262
if text_changed:
263263
await document.apply_full_change(text_document.version, normalized_text)
264264

@@ -326,7 +326,7 @@ async def _text_document_did_save(
326326
if text is not None:
327327
normalized_text = self._normalize_line_endings(text)
328328

329-
text_changed = document.text != normalized_text
329+
text_changed = await document.text() != normalized_text
330330
if text_changed:
331331
await document.save(None, text)
332332
create_sub_task(

robotcode/language_server/common/text_document.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import collections
44
import inspect
55
import io
6-
import threading
76
import weakref
87
from typing import Any, Awaitable, Callable, Dict, List, Optional, TypeVar, Union, cast
98

10-
from ...utils.async_tools import async_event, create_sub_task
9+
from ...utils.async_tools import Lock, async_event, create_sub_task
1110
from ...utils.logging import LoggingDescriptor
1211
from ...utils.uri import Uri
1312
from .lsp_types import DocumentUri, Range
@@ -24,7 +23,7 @@ class CacheEntry:
2423
def __init__(self) -> None:
2524
self.data: Any = None
2625
self.has_data: bool = False
27-
self.lock = threading.RLock()
26+
self.lock = Lock()
2827

2928

3029
class TextDocument:
@@ -39,7 +38,7 @@ def __init__(
3938
) -> None:
4039
super().__init__()
4140

42-
self._lock = threading.RLock()
41+
self._lock = Lock()
4342
self.document_uri = document_uri
4443
self.uri = Uri(self.document_uri).normalized()
4544
self.language_id = language_id
@@ -71,9 +70,11 @@ def __repr__(self) -> str: # pragma: no cover
7170
f")"
7271
)
7372

74-
@property
75-
def text(self) -> str:
76-
with self._lock:
73+
def text_sync(self) -> str:
74+
return self._text
75+
76+
async def text(self) -> str:
77+
async with self._lock:
7778
return self._text
7879

7980
async def save(self, version: Optional[int], text: Optional[str]) -> None:
@@ -87,13 +88,13 @@ async def revert(self, version: Optional[int]) -> bool:
8788

8889
@_logger.call
8990
async def apply_none_change(self) -> None:
90-
with self._lock:
91+
async with self._lock:
9192
self._lines = None
9293
await self._invalidate_cache()
9394

9495
@_logger.call
9596
async def apply_full_change(self, version: Optional[int], text: Optional[str], *, save: bool = False) -> None:
96-
with self._lock:
97+
async with self._lock:
9798
if version is not None:
9899
self._version = version
99100
if text is not None:
@@ -105,7 +106,7 @@ async def apply_full_change(self, version: Optional[int], text: Optional[str], *
105106

106107
@_logger.call
107108
async def apply_incremental_change(self, version: Optional[int], range: Range, text: str) -> None:
108-
with self._lock:
109+
async with self._lock:
109110
try:
110111
if version is not None:
111112
self._version = version
@@ -146,10 +147,11 @@ def __get_lines(self) -> List[str]:
146147
return self._lines
147148

148149
async def get_lines(self) -> List[str]:
149-
if self._lines is None:
150-
with self._lock:
150+
async with self._lock:
151+
if self._lines is None:
151152
return self.__get_lines()
152-
return self._lines
153+
154+
return self._lines
153155

154156
@async_event
155157
async def cache_invalidate(sender) -> None: # NOSONAR
@@ -166,19 +168,19 @@ async def _invalidate_cache(self) -> None:
166168

167169
@_logger.call
168170
async def invalidate_cache(self) -> None:
169-
with self._lock:
171+
async with self._lock:
170172
await self._invalidate_cache()
171173

172174
async def _invalidate_data(self) -> None:
173175
self._data.clear()
174176

175177
@_logger.call
176178
async def invalidate_data(self) -> None:
177-
with self._lock:
179+
async with self._lock:
178180
await self._invalidate_data()
179181

180182
async def __remove_cache_entry_safe(self, _ref: Any) -> None:
181-
with self._lock:
183+
async with self._lock:
182184
if _ref in self._cache:
183185
self._cache.pop(_ref)
184186

@@ -218,7 +220,7 @@ async def get_cache(
218220

219221
e = self._cache[reference]
220222

221-
with e.lock:
223+
async with e.lock:
222224
if not e.has_data:
223225
e.data = await entry(self, *args, **kwargs)
224226
e.has_data = True
@@ -250,5 +252,5 @@ async def _clear(self) -> None:
250252

251253
@_logger.call
252254
async def clear(self) -> None:
253-
with self._lock:
255+
async with self._lock:
254256
await self._clear()

robotcode/language_server/robotframework/diagnostics/analyzer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ async def visit(self, node: ast.AST) -> None:
240240
finally:
241241
self.node_stack = self.node_stack[:-1]
242242

243-
@staticmethod
244-
async def get_ignored_lines(document: TextDocument) -> List[int]:
245-
return await document.get_cache(Analyzer.__get_ignored_lines)
243+
# @staticmethod
244+
# async def get_ignored_lines(document: TextDocument) -> List[int]:
245+
# return await document.get_cache(Analyzer.__get_ignored_lines)
246246

247247
@staticmethod
248-
async def __get_ignored_lines(document: TextDocument) -> List[int]:
248+
async def get_ignored_lines(document: TextDocument) -> List[int]:
249249
result = []
250250
lines = await document.get_lines()
251251
for line_no, line in enumerate(lines):

robotcode/language_server/robotframework/diagnostics/imports_manager.py

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,13 @@ async def is_valid(self) -> bool:
262262
return self._lib_doc is not None
263263

264264
async def get_libdoc(self) -> LibraryDoc:
265-
if self._lib_doc is None:
266-
async with self._lock:
267-
if self._lib_doc is None:
268-
await self._update()
265+
async with self._lock:
266+
if self._lib_doc is None:
267+
await self._update()
269268

270-
assert self._lib_doc is not None
269+
assert self._lib_doc is not None
271270

272-
return self._lib_doc
271+
return self._lib_doc
273272

274273

275274
@dataclass()
@@ -344,9 +343,8 @@ async def is_valid(self) -> bool:
344343
return self._document is not None
345344

346345
async def get_document(self) -> TextDocument:
347-
if self._document is None:
348-
async with self._lock:
349-
await self._get_document()
346+
async with self._lock:
347+
await self._get_document()
350348

351349
assert self._document is not None
352350

@@ -368,11 +366,11 @@ async def _get_namespace(self) -> Namespace:
368366
return await self.parent.parent_protocol.documents_cache.get_resource_namespace(await self._get_document())
369367

370368
async def get_libdoc(self) -> LibraryDoc:
371-
if self._lib_doc is None:
372-
async with self._lock:
373-
if self._lib_doc is None:
374-
self._lib_doc = await (await self._get_namespace()).get_library_doc()
375-
return self._lib_doc
369+
async with self._lock:
370+
if self._lib_doc is None:
371+
self._lib_doc = await (await self._get_namespace()).get_library_doc()
372+
373+
return self._lib_doc
376374

377375

378376
@dataclass()
@@ -446,14 +444,13 @@ async def is_valid(self) -> bool:
446444
return self._lib_doc is not None
447445

448446
async def get_libdoc(self) -> VariablesDoc:
449-
if self._lib_doc is None:
450-
async with self._lock:
451-
if self._lib_doc is None:
452-
await self._update()
447+
async with self._lock:
448+
if self._lib_doc is None:
449+
await self._update()
453450

454-
assert self._lib_doc is not None
451+
assert self._lib_doc is not None
455452

456-
return self._lib_doc
453+
return self._lib_doc
457454

458455

459456
class ImportsManager:
@@ -833,12 +830,11 @@ async def _get_libdoc() -> LibraryDoc:
833830

834831
entry_key = _LibrariesEntryKey(source, args)
835832

836-
if entry_key not in self._libaries:
837-
async with self._libaries_lock:
838-
if entry_key not in self._libaries:
839-
self._libaries[entry_key] = _LibrariesEntry(
840-
name, args, self, _get_libdoc, ignore_reference=sentinel is None
841-
)
833+
async with self._libaries_lock:
834+
if entry_key not in self._libaries:
835+
self._libaries[entry_key] = _LibrariesEntry(
836+
name, args, self, _get_libdoc, ignore_reference=sentinel is None
837+
)
842838

843839
entry = self._libaries[entry_key]
844840

@@ -1016,10 +1012,9 @@ async def _get_libdoc() -> VariablesDoc:
10161012

10171013
entry_key = _VariablesEntryKey(source, args)
10181014

1019-
if entry_key not in self._variables:
1020-
async with self._variables_lock:
1021-
if entry_key not in self._variables:
1022-
self._variables[entry_key] = _VariablesEntry(name, args, self, _get_libdoc)
1015+
async with self._variables_lock:
1016+
if entry_key not in self._variables:
1017+
self._variables[entry_key] = _VariablesEntry(name, args, self, _get_libdoc)
10231018

10241019
entry = self._variables[entry_key]
10251020

@@ -1050,11 +1045,10 @@ async def _get_document() -> TextDocument:
10501045

10511046
entry_key = _ResourcesEntryKey(source)
10521047

1053-
if entry_key not in self._resources:
1054-
async with self._resources_lock:
1048+
async with self._resources_lock:
10551049

1056-
if entry_key not in self._resources:
1057-
self._resources[entry_key] = _ResourcesEntry(name, self, _get_document)
1050+
if entry_key not in self._resources:
1051+
self._resources[entry_key] = _ResourcesEntry(name, self, _get_document)
10581052

10591053
entry = self._resources[entry_key]
10601054

robotcode/language_server/robotframework/diagnostics/library_doc.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,9 @@ def find_library(
11311131
)[0]
11321132

11331133

1134-
def get_robot_library_html_doc_str(name: str, working_dir: str = ".", base_dir: str = ".") -> str:
1134+
def get_robot_library_html_doc_str(
1135+
name: str, working_dir: str = ".", base_dir: str = ".", theme: Optional[str] = None
1136+
) -> str:
11351137
from robot.libdocpkg import LibraryDocumentation
11361138
from robot.libdocpkg.htmlwriter import LibdocHtmlWriter
11371139

@@ -1145,7 +1147,10 @@ def get_robot_library_html_doc_str(name: str, working_dir: str = ".", base_dir:
11451147
robot_libdoc = LibraryDocumentation(name)
11461148
robot_libdoc.convert_docs_to_html()
11471149
with io.StringIO() as output:
1148-
writer = LibdocHtmlWriter()
1150+
if get_robot_version() > (6, 0):
1151+
writer = LibdocHtmlWriter(theme=theme)
1152+
else:
1153+
writer = LibdocHtmlWriter()
11491154
writer.write(robot_libdoc, output)
11501155

11511156
return output.getvalue()

0 commit comments

Comments
 (0)