Skip to content

Commit

Permalink
Migrate ContentAssistHistory to IEclipsePreferences
Browse files Browse the repository at this point in the history
Long deprecated o.e.core.runtime.Preferences class would better not be
used anymore.
  • Loading branch information
akurtakov committed Jan 21, 2025
1 parent 1b3b080 commit daaa3f3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2020 IBM Corporation and others.
* Copyright (c) 2005, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -25,7 +25,8 @@
import org.junit.Rule;
import org.junit.Test;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -228,14 +229,14 @@ public void testReadOnlyHistory() {
}

@Test
public void testLoadStore() throws Exception {
public void testLoadStoreIEclipsePreferences() throws Exception {
ContentAssistHistory history= new ContentAssistHistory();

history.remember(fgListT, fgArrayListT);
history.remember(fgCharSequenceT, fgStringT);


Preferences prefs= new Preferences();
IEclipsePreferences prefs= InstanceScope.INSTANCE.getNode("org.eclipse.jdt.text.testsa");
String key= "myKey";
ContentAssistHistory.store(history, prefs, key);
ContentAssistHistory loaded= ContentAssistHistory.load(prefs, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public void stop(BundleContext context) throws Exception {
}

if (fContentAssistHistory != null) {
ContentAssistHistory.store(fContentAssistHistory, getPluginPreferences(), PreferenceConstants.CODEASSIST_LRU_HISTORY);
ContentAssistHistory.store(fContentAssistHistory, InstanceScope.INSTANCE.getNode(JavaPlugin.getPluginId()), PreferenceConstants.CODEASSIST_LRU_HISTORY);
fContentAssistHistory= null;
}

Expand Down Expand Up @@ -1045,7 +1045,7 @@ public ContentAssistHistory getContentAssistHistory() {
return fContentAssistHistory;
}
try {
fContentAssistHistory= ContentAssistHistory.load(getPluginPreferences(), PreferenceConstants.CODEASSIST_LRU_HISTORY);
fContentAssistHistory= ContentAssistHistory.load(InstanceScope.INSTANCE.getNode(JavaPlugin.getPluginId()), PreferenceConstants.CODEASSIST_LRU_HISTORY);
} catch (CoreException x) {
log(x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;

import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -455,12 +455,12 @@ private IProgressMonitor getProgressMonitor() {
* @param preferences the preferences to store the history into
* @param key the key under which to store the history
* @throws CoreException if serialization fails
* @see #load(Preferences, String) on how to restore a history stored by this method
* @see #load(IEclipsePreferences, String) on how to restore a history stored by this method
*/
public static void store(ContentAssistHistory history, Preferences preferences, String key) throws CoreException {
public static void store(ContentAssistHistory history, IEclipsePreferences preferences, String key) throws CoreException {
StringWriter writer= new StringWriter();
new ReaderWriter().store(history, new StreamResult(writer));
preferences.setValue(key, writer.toString());
preferences.put(key, writer.toString());
}

/**
Expand All @@ -471,11 +471,11 @@ public static void store(ContentAssistHistory history, Preferences preferences,
* @return the deserialized history, or <code>null</code> if there is nothing stored under the
* given key
* @throws CoreException if deserialization fails
* @see #store(ContentAssistHistory, Preferences, String) on how to store a history such that it
* @see #store(ContentAssistHistory, IEclipsePreferences, String) on how to store a history such that it
* can be read by this method
*/
public static ContentAssistHistory load(Preferences preferences, String key) throws CoreException {
String value= preferences.getString(key);
public static ContentAssistHistory load(IEclipsePreferences preferences, String key) throws CoreException {
String value= preferences.get(key, ""); //$NON-NLS-1$
if (value != null && value.length() > 0) {
return new ReaderWriter().load(new InputSource(new StringReader(value)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -26,6 +26,7 @@
import org.eclipse.swt.graphics.RGB;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;

import org.eclipse.jface.action.Action;
Expand Down Expand Up @@ -3776,7 +3777,7 @@ private PreferenceConstants() {
* Value is an XML encoded version of the history.
* </p>
*
* @see org.eclipse.jdt.internal.ui.text.java.ContentAssistHistory#load(org.eclipse.core.runtime.Preferences, String)
* @see org.eclipse.jdt.internal.ui.text.java.ContentAssistHistory#load(IEclipsePreferences, String)
* @since 3.2
*/
public static final String CODEASSIST_LRU_HISTORY= "content_assist_lru_history"; //$NON-NLS-1$
Expand Down

0 comments on commit daaa3f3

Please sign in to comment.