1919import java .util .Map ;
2020import java .util .Optional ;
2121
22+ import org .eclipse .cdt .core .build .CBuildConfiguration ;
23+ import org .eclipse .cdt .core .build .ICBuildConfiguration ;
24+ import org .eclipse .cdt .core .build .ICBuildConfigurationManager ;
2225import org .eclipse .cdt .core .cdtvariables .CdtVariableException ;
2326import org .eclipse .cdt .core .settings .model .CProjectDescriptionEvent ;
2427import org .eclipse .cdt .core .settings .model .ICConfigurationDescription ;
3639import org .eclipse .core .runtime .Platform ;
3740import org .eclipse .core .runtime .Status ;
3841import org .osgi .service .component .annotations .Component ;
42+ import org .osgi .service .component .annotations .Reference ;
3943import org .yaml .snakeyaml .Yaml ;
4044import org .yaml .snakeyaml .scanner .ScannerException ;
4145
@@ -56,6 +60,9 @@ public class ClangdConfigurationFileManager implements ClangdCProjectDescription
5660 private static final String SET_COMPILATION_DB = COMPILE_FLAGS + ": {" + COMPILATTION_DATABASE + ": %s}" ; //$NON-NLS-1$ //$NON-NLS-2$
5761 private static final String EMPTY = "" ; //$NON-NLS-1$
5862
63+ @ Reference
64+ private ICBuildConfigurationManager build ;
65+
5966 @ Override
6067 public void handleEvent (CProjectDescriptionEvent event , MacroResolver macroResolver ) {
6168 setCompilationDatabasePath (event .getProject (), event .getNewCProjectDescription (), macroResolver );
@@ -105,19 +112,45 @@ private String getRelativeDatabasePath(IProject project, ICProjectDescription ne
105112 if (project != null && newCProjectDescription != null ) {
106113 ICConfigurationDescription config = newCProjectDescription .getDefaultSettingConfiguration ();
107114 var cwdBuilder = config .getBuildSetting ().getBuilderCWD ();
115+ var projectLocation = project .getLocation ().addTrailingSeparator ().toOSString ();
108116 if (cwdBuilder != null ) {
109- var projectLocation = project .getLocation ().addTrailingSeparator ().toOSString ();
110117 try {
111118 var cwdString = macroResolver .resolveValue (cwdBuilder .toOSString (), EMPTY , null , config );
112119 return cwdString .replace (projectLocation , EMPTY );
113120 } catch (CdtVariableException e ) {
114121 Platform .getLog (getClass ()).log (e .getStatus ());
115122 }
123+ } else {
124+ //it is probably a cmake project:
125+ return buildConfiguration (project )//
126+ .filter (CBuildConfiguration .class ::isInstance )//
127+ .map (bc -> {
128+ try {
129+ return ((CBuildConfiguration ) bc ).getBuildContainer ();
130+ } catch (CoreException e ) {
131+ Platform .getLog (getClass ()).log (e .getStatus ());
132+ }
133+ return null ;
134+ })//
135+ .map (c -> c .getLocation ())//
136+ .map (l -> l .toOSString ().replace (projectLocation , EMPTY )).orElse (EMPTY );
116137 }
117138 }
118139 return EMPTY ;
119140 }
120141
142+ private Optional <ICBuildConfiguration > buildConfiguration (IResource initial ) {
143+ try {
144+ var active = initial .getProject ().getActiveBuildConfig ();
145+ if (active != null && build != null ) {
146+ return Optional .ofNullable (build .getBuildConfiguration (active ));
147+ }
148+ } catch (CoreException e ) {
149+ Platform .getLog (getClass ()).error (e .getMessage (), e );
150+ }
151+ return Optional .empty ();
152+ }
153+
121154 /**
122155 * Set the <code>CompilationDatabase</code> entry in the .clangd file in the given project root.
123156 * The file will be created, if it's not existing.
0 commit comments