@@ -198,18 +198,46 @@ internal void DoImport()
198198
199199 private void ImportStyles ( string importStylesLocation )
200200 {
201- var stylesToRemove = _cache . LangProject . StylesOC . Where ( style => ! UnsupportedStyles . Contains ( style . Name ) ) ;
201+ var stylesToRemove = _cache . LangProject . StylesOC . Where ( style => ! UnsupportedStyles . Contains ( style . Name ) ) . ToArray ( ) ;
202202
203- // For LT-18267, record basedon and next properties of styles not
203+ // For LT-18267, record basedOn and next properties of styles not
204204 // being exported, so they can be reconnected to the imported
205205 // styles of the same name.
206- var preimportStyleLinks = _cache . LangProject . StylesOC . Where ( style => UnsupportedStyles . Contains ( style . Name ) ) . ToDictionary (
206+ var preImportStyleLinks = _cache . LangProject . StylesOC . Where ( style => UnsupportedStyles . Contains ( style . Name ) ) . ToDictionary (
207207 style => style . Name ,
208- style => new
208+ style => new Tuple < string , string > (
209+ style . BasedOnRA ? . Name ,
210+ style . NextRA ? . Name
211+ ) ) ;
212+ RemoveStylesAndSave ( stylesToRemove ) ;
213+ try
214+ {
215+ // Import styles
216+ NonUndoableUnitOfWorkHelper . DoSomehow ( _cache . ActionHandlerAccessor , ( ) =>
209217 {
210- BasedOn = style . BasedOnRA == null ? null : style . BasedOnRA . Name ,
211- Next = style . NextRA == null ? null : style . NextRA . Name
218+ // ReSharper disable once ObjectCreationAsStatement -- The FlexStylesXmlAccessor constructor does the work of importing.
219+ new FlexStylesXmlAccessor ( _cache . LangProject . LexDbOA , true , importStylesLocation ) ;
220+ ReconnectStyles ( preImportStyleLinks ) ;
212221 } ) ;
222+ }
223+ catch ( InstallationException e )
224+ {
225+ // If we fail to import the styles, remove any that were imported, then restore the ones we removed (LT-20393).
226+ RemoveStylesAndSave ( _cache . LangProject . StylesOC . Where ( style => ! UnsupportedStyles . Contains ( style . Name ) ) ) ;
227+ NonUndoableUnitOfWorkHelper . DoSomehow ( _cache . ActionHandlerAccessor , ( ) =>
228+ {
229+ foreach ( var style in stylesToRemove )
230+ {
231+ _cache . LangProject . StylesOC . Add ( style ) ;
232+ ReconnectStyles ( preImportStyleLinks ) ;
233+ }
234+ } ) ;
235+ throw ;
236+ }
237+ }
238+
239+ private void RemoveStylesAndSave ( IEnumerable < IStStyle > stylesToRemove )
240+ {
213241 NonUndoableUnitOfWorkHelper . DoSomehow ( _cache . ActionHandlerAccessor , ( ) =>
214242 {
215243 // Before importing styles, remove all the current styles, except
@@ -223,23 +251,17 @@ private void ImportStyles(string importStylesLocation)
223251 // Be sure that the Remove action is committed and saved to disk before we re-import styles with the same guid.
224252 // If we don't then the changes won't be noticed as the Styles will be marked as transient and won't be saved.
225253 _cache . ActionHandlerAccessor . Commit ( ) ;
226- // Import styles
227- NonUndoableUnitOfWorkHelper . DoSomehow ( _cache . ActionHandlerAccessor , ( ) =>
228- {
229- // REVIEW (Hasso) 2026.01: do we really need to re-import styles?
230- // ReSharper disable once UnusedVariable -- The FlexStylesXmlAccessor constructor does the work of importing.
231- var stylesAccessor = new FlexStylesXmlAccessor ( _cache . LangProject . LexDbOA , true , importStylesLocation ) ;
232-
233- var postimportStylesToReconnect = _cache . LangProject . StylesOC . Where ( style => UnsupportedStyles . Contains ( style . Name ) ) ;
234-
235- postimportStylesToReconnect . ForEach ( postimportStyleToRewire =>
236- {
237- var correspondingPreImportStyleInfo = preimportStyleLinks [ postimportStyleToRewire . Name ] ;
254+ }
238255
239- postimportStyleToRewire . BasedOnRA = _cache . LangProject . StylesOC . FirstOrDefault ( style => style . Name == correspondingPreImportStyleInfo . BasedOn ) ;
256+ private void ReconnectStyles ( Dictionary < string , Tuple < string , string > > preImportStyleLinks )
257+ {
258+ var postImportStylesToReconnect = _cache . LangProject . StylesOC . Where ( style => UnsupportedStyles . Contains ( style . Name ) ) ;
240259
241- postimportStyleToRewire . NextRA = _cache . LangProject . StylesOC . FirstOrDefault ( style => style . Name == correspondingPreImportStyleInfo . Next ) ;
242- } ) ;
260+ postImportStylesToReconnect . ForEach ( styleToReconnect =>
261+ {
262+ var correspondingPreImportStyleInfo = preImportStyleLinks [ styleToReconnect . Name ] ;
263+ styleToReconnect . BasedOnRA = _cache . LangProject . StylesOC . FirstOrDefault ( style => style . Name == correspondingPreImportStyleInfo . Item1 ) ;
264+ styleToReconnect . NextRA = _cache . LangProject . StylesOC . FirstOrDefault ( style => style . Name == correspondingPreImportStyleInfo . Item2 ) ;
243265 } ) ;
244266 }
245267
0 commit comments