forked from eclipse-cdt/cdt-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: .clang-format file handling (eclipse-cdt#364)
refactor: .clang-format file handling - Mark ICLanguageServerProvider2 as deprecated since it's not a good way to do things here. - Creates the .clang-format file when a C/C++ source file gets opened or edited in the LSP based editor. - Use a creation-and-open job for the .clang-format file when it gets accessed via the project properties UI. This solves the problem that the file creation has to be finished before the file can be opened. - Create interface ClangFormatFile to allow vendors to provide their own default .clang-format file TODO: enable automatic creation of the .clang-format file via project properties setting.
- Loading branch information
1 parent
160adf6
commit 1e6f3d0
Showing
11 changed files
with
190 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...lsp.clangd/OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangFormatFileHandler.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.cdt.lsp.clangd.internal.config.ClangFormatFileHandler"> | ||
<property name="service.ranking" type="Integer" value="0"/> | ||
<service> | ||
<provide interface="org.eclipse.cdt.lsp.clangd.ClangFormatFile"/> | ||
</service> | ||
<implementation class="org.eclipse.cdt.lsp.clangd.internal.config.ClangFormatFileHandler"/> | ||
</scr:component> |
34 changes: 34 additions & 0 deletions
34
bundles/org.eclipse.cdt.lsp.clangd/src/org/eclipse/cdt/lsp/clangd/ClangFormatFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* See git history | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.cdt.lsp.clangd; | ||
|
||
import org.eclipse.core.resources.IProject; | ||
|
||
/** | ||
* @since 2.1 | ||
*/ | ||
public interface ClangFormatFile { | ||
|
||
/** | ||
* Opens the .clang-format file in the given project. Creates a file with default values, if not yet existing prior to the opening. | ||
* @param formatFile | ||
*/ | ||
void openClangFormatFile(IProject project); | ||
|
||
/** | ||
* Creates a new .clang-format file with default settings in the project root directory if not yet existing. | ||
* @param project | ||
*/ | ||
void createClangFormatFile(IProject project); | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...pse.cdt.lsp.clangd/src/org/eclipse/cdt/lsp/clangd/internal/config/ClangFormatMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* See git history | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.cdt.lsp.clangd.internal.config; | ||
|
||
import org.eclipse.cdt.lsp.clangd.ClangFormatFile; | ||
import org.eclipse.cdt.lsp.plugin.LspPlugin; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.runtime.Platform; | ||
import org.eclipse.ui.IPartListener2; | ||
import org.eclipse.ui.IWindowListener; | ||
import org.eclipse.ui.IWorkbenchPartReference; | ||
import org.eclipse.ui.IWorkbenchWindow; | ||
import org.eclipse.ui.PlatformUI; | ||
|
||
public final class ClangFormatMonitor implements IPartListener2, IWindowListener { | ||
private ClangFormatFile clangFormat; | ||
|
||
public ClangFormatMonitor start() { | ||
if (PlatformUI.isWorkbenchRunning()) { | ||
var workbench = PlatformUI.getWorkbench(); | ||
clangFormat = workbench.getService(ClangFormatFile.class); | ||
if (clangFormat == null) { | ||
Platform.getLog(getClass()).error("Cannot get ClangFormatFile service."); //$NON-NLS-1$ | ||
return this; | ||
} | ||
workbench.addWindowListener(this); | ||
|
||
// Ensure existing windows get connected | ||
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); | ||
for (int i = 0, length = windows.length; i < length; i++) { | ||
windows[i].getPartService().addPartListener(this); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public void stop() { | ||
PlatformUI.getWorkbench().removeWindowListener(this); | ||
} | ||
|
||
@Override | ||
public void partActivated(IWorkbenchPartReference partRef) { | ||
createClangFormatFile(partRef); | ||
} | ||
|
||
@Override | ||
public void windowActivated(IWorkbenchWindow window) { | ||
// do nothing | ||
} | ||
|
||
@Override | ||
public void windowDeactivated(IWorkbenchWindow window) { | ||
// do nothing | ||
} | ||
|
||
@Override | ||
public void windowClosed(IWorkbenchWindow window) { | ||
window.getPartService().removePartListener(this); | ||
} | ||
|
||
@Override | ||
public void windowOpened(IWorkbenchWindow window) { | ||
window.getPartService().addPartListener(this); | ||
} | ||
|
||
private void createClangFormatFile(IWorkbenchPartReference partRef) { | ||
if (isLspCEditor(partRef)) { | ||
var file = partRef.getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class); | ||
|
||
if (file == null) { | ||
return; | ||
} | ||
clangFormat.createClangFormatFile(file.getProject()); | ||
} | ||
} | ||
|
||
private boolean isLspCEditor(IWorkbenchPartReference partRef) { | ||
return partRef != null ? LspPlugin.LSP_C_EDITOR_ID.equals(partRef.getId()) : false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.