Skip to content

Commit

Permalink
[eclipse-cdt#182] Fix ResourceException when 'Save As...' on text fil…
Browse files Browse the repository at this point in the history
…e in workspace

IFileBuffer.getContentType() throws an exception when the underlying
resource does not exists.

fixes eclipse-cdt#182
  • Loading branch information
ghentschke committed Aug 18, 2023
1 parent b596618 commit 7aa027b
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ class SymbolsContainer {
private HashMap<URI, SymbolsContainer> cachedSymbols = new HashMap<>();

private final IFileBufferListener fileBufferListener = new FileBufferListenerAdapter() {

@Override
public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {
try {
if (isDirty && isCElement(buffer.getContentType())) {
// Note: resourceExists must be called prior to buffer.getContentType(),
// because getContentType() throws an exception when the underlying resource does not exists.
// This can be the case if 'Save as..' has been performed on a file.
// Then isDirty is true and dirtyStateChanged gets called for the new, not yet existing file.
if (isDirty && resourceExists(buffer) && isCElement(buffer.getContentType())) {
var uri = LSPEclipseUtils.toUri(buffer);
if (uri != null) {
var cachedSymbol = cachedSymbols.get(uri);
Expand All @@ -84,6 +89,10 @@ public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {

}

private boolean resourceExists(IFileBuffer buffer) {
return Optional.ofNullable(buffer.getLocation()).map(l -> l.toFile().exists()).orElse(false);
}

private boolean isCElement(IContentType contentType) {
if (contentType == null) {
return false;
Expand Down

0 comments on commit 7aa027b

Please sign in to comment.