Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.lsp4j.FormattingOptions;

import com.microsoft.copilot.eclipse.core.CopilotCore;
Expand All @@ -26,6 +28,9 @@ public class FormatOptionProvider {
private static final String CPP_LANGUAGE_ID = "cpp";
private static final String[] CPP_LANGUAGE_EXTENSIONS = new String[] { "cpp", "c++", "cc", "cp", "cxx", "h", "h++",
"hh", ".hpp", ".hxx", ".inc", ".inl", ".ipp", ".tcc", ".tpp" };
private static final String EDITOR_PREF_NODE = "org.eclipse.ui.editors";
private static final String TAB_WIDTH_KEY = "tabWidth";
private static final String SPACES_FOR_TABS_KEY = "spacesForTabs";
private static final boolean DEFAULT_USE_SPACE = LanguageFormatReader.PREFERENCE_DEFAULT_TAB_CHAR.equals("space");
private static final int DEFAULT_TAB_SIZE = LanguageFormatReader.PREFERENCE_DEFAULT_TAB_SIZE;

Expand Down Expand Up @@ -110,14 +115,46 @@ private FormattingOptions getLanguageFormat(IFile file) {
if (languageFormatReaderForProject == null) {
languageFormatReaderForProject = loadFormatReaderForTheProject(languageId, project);
if (languageFormatReaderForProject == null) {
return new FormattingOptions(DEFAULT_TAB_SIZE, DEFAULT_USE_SPACE);
return getEclipseTextEditorFormattingOptions();
}
projectToLanguageFormatReaderMap.put(project, languageFormatReaderForProject);
}

return languageFormatReaderForProject.getFormattingOptions();
}

/**
* Attempts to read the Eclipse default text editor preferences for tab size and spaces-for-tabs.
* Falls back to hardcoded defaults if the preferences cannot be read.
*/
private FormattingOptions getEclipseTextEditorFormattingOptions() {
try {
IPreferencesService service = Platform.getPreferencesService();

boolean useSpaces = service.getBoolean(
EDITOR_PREF_NODE,
SPACES_FOR_TABS_KEY,
DEFAULT_USE_SPACE,
null
);

int tabSize = service.getInt(
EDITOR_PREF_NODE,
TAB_WIDTH_KEY,
DEFAULT_TAB_SIZE,
null
);

return new FormattingOptions(tabSize, useSpaces);
} catch (Exception e) {
CopilotCore.LOGGER.info(
"Failed to read Eclipse text editor preferences, using defaults");
return new FormattingOptions(DEFAULT_TAB_SIZE, DEFAULT_USE_SPACE);
}
}



private LanguageFormatReader loadFormatReaderForTheProject(String languageId, IProject project) {
switch (languageId) {
case JAVA_LANGUAGE_ID:
Expand All @@ -129,4 +166,4 @@ private LanguageFormatReader loadFormatReaderForTheProject(String languageId, IP
return null;
}
}
}
}