forked from eclipse-lemminx/lemminx
-
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.
Provide basic experimental formatter which supports invalid XML
Fixes eclipse-lemminx#1195 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
82abfce
commit 099e2a7
Showing
40 changed files
with
7,009 additions
and
1,096 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
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
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
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
70 changes: 70 additions & 0 deletions
70
...clipse/lemminx/extensions/contentmodel/participants/ContentModelFormatterParticipant.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,70 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.contentmodel.participants; | ||
|
||
import java.util.Collection; | ||
|
||
import org.eclipse.lemminx.dom.DOMElement; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.CMDocument; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.CMElementDeclaration; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager; | ||
import org.eclipse.lemminx.services.extensions.format.IFormatterParticipant; | ||
import org.eclipse.lemminx.services.format.FormatElementCategory; | ||
import org.eclipse.lemminx.services.format.XMLFormattingConstraints; | ||
import org.eclipse.lemminx.settings.SharedSettings; | ||
|
||
/** | ||
* Formatter participant which uses XSD/DTD grammar information to know the | ||
* {@link FormatElementCategory} of a given element. | ||
* | ||
* <p> | ||
* | ||
* This participant is enabled when 'xml.format.grammarAwareFormatting' setting | ||
* is set to true. | ||
* | ||
* </p> | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class ContentModelFormatterParticipant implements IFormatterParticipant { | ||
|
||
private final ContentModelManager contentModelManager; | ||
|
||
public ContentModelFormatterParticipant(ContentModelManager contentModelManager) { | ||
this.contentModelManager = contentModelManager; | ||
} | ||
|
||
@Override | ||
public FormatElementCategory getFormatElementCategory(DOMElement element, | ||
XMLFormattingConstraints parentConstraints, SharedSettings sharedSettings) { | ||
boolean enabled = sharedSettings.getFormattingSettings().isGrammarAwareFormatting(); | ||
if (!enabled) { | ||
return null; | ||
} | ||
|
||
Collection<CMDocument> cmDocuments = contentModelManager.findCMDocument(element); | ||
for (CMDocument cmDocument : cmDocuments) { | ||
CMElementDeclaration cmElement = cmDocument.findCMElement(element); | ||
if (cmElement != null) { | ||
if (cmElement.isStringType()) { | ||
return FormatElementCategory.PreserveSpace; | ||
} | ||
if (cmElement.isMixedContent()) { | ||
return FormatElementCategory.MixedContent; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
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.