-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parse and display indentation rules
- Loading branch information
Showing
10 changed files
with
379 additions
and
70 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
45 changes: 45 additions & 0 deletions
45
...n/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/model/IndentForEnter.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,45 @@ | ||
/** | ||
* Copyright (c) 2024 Vegard IT GmbH and others. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Initial code from https://github.com/microsoft/vscode/ | ||
* Initial copyright Copyright (C) Microsoft Corporation. All rights reserved. | ||
* Initial license: MIT | ||
* | ||
* Contributors: | ||
* - Microsoft Corporation: Initial code, written in TypeScript, licensed under MIT license | ||
* - Sebastian Thomschke - translation and adaptation to Java | ||
*/ | ||
package org.eclipse.tm4e.languageconfiguration.internal.model; | ||
|
||
import org.eclipse.tm4e.core.internal.utils.StringUtils; | ||
|
||
/** | ||
* Describes indentation rules for a language. | ||
* | ||
* @see <a href= | ||
* "https://github.com/microsoft/vscode/blob/8e2ec5a7ee1ae5500c645c05145359f2a814611c/src/vs/editor/common/languages/autoIndent.ts#L278"> | ||
* github.com/microsoft/vscode/blob/main/src/vs/editor/common/languages/autoIndent.ts#L278</a> | ||
*/ | ||
public class IndentForEnter { | ||
|
||
public final String beforeEnter; | ||
public final String afterEnter; | ||
|
||
public IndentForEnter(final String beforeEnter, final String afterEnter) { | ||
this.beforeEnter = beforeEnter; | ||
this.afterEnter = afterEnter; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return StringUtils.toString(this, sb -> sb | ||
.append("beforeEnter=").append(beforeEnter).append(", ") | ||
.append("afterEnter=").append(afterEnter)); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...src/main/java/org/eclipse/tm4e/languageconfiguration/internal/model/IndentationRules.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,58 @@ | ||
/** | ||
* Copyright (c) 2024 Vegard IT GmbH and others. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Initial code from https://github.com/microsoft/vscode/ | ||
* Initial copyright Copyright (C) Microsoft Corporation. All rights reserved. | ||
* Initial license: MIT | ||
* | ||
* Contributors: | ||
* - Microsoft Corporation: Initial code, written in TypeScript, licensed under MIT license | ||
* - Sebastian Thomschke - translation and adaptation to Java | ||
*/ | ||
package org.eclipse.tm4e.languageconfiguration.internal.model; | ||
|
||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
/** | ||
* Describes indentation rules for a language. | ||
* | ||
* @see <a href= | ||
* "https://github.com/microsoft/vscode/blob/8e2ec5a7ee1ae5500c645c05145359f2a814611c/src/vs/editor/common/languages/languageConfiguration.ts#L105"> | ||
* github.com/microsoft/vscode/blob/main/src/vs/editor/common/languages/languageConfiguration.ts#L105</a> | ||
*/ | ||
public class IndentationRules { | ||
|
||
/** | ||
* If a line matches this pattern, then all the lines after it should be unindented once (until another rule matches). | ||
*/ | ||
public final RegExPattern decreaseIndentPattern; | ||
|
||
/** | ||
* If a line matches this pattern, then all the lines after it should be indented once (until another rule matches). | ||
*/ | ||
public final RegExPattern increaseIndentPattern; | ||
|
||
/** | ||
* If a line matches this pattern, then **only the next line** after it should be indented once. | ||
*/ | ||
public final @Nullable RegExPattern indentNextLinePattern; | ||
|
||
/** | ||
* If a line matches this pattern, then its indentation should not be changed and it should not be evaluated against the other rules. | ||
*/ | ||
public final @Nullable RegExPattern unIndentedLinePattern; | ||
|
||
public IndentationRules(final RegExPattern decreaseIndentPattern, final RegExPattern increaseIndentPattern, | ||
final @Nullable RegExPattern indentNextLinePattern, final @Nullable RegExPattern unIndentedLinePattern) { | ||
this.decreaseIndentPattern = decreaseIndentPattern; | ||
this.increaseIndentPattern = increaseIndentPattern; | ||
this.indentNextLinePattern = indentNextLinePattern; | ||
this.unIndentedLinePattern = unIndentedLinePattern; | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
...ain/java/org/eclipse/tm4e/languageconfiguration/internal/supports/IndentRulesSupport.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,79 @@ | ||
/** | ||
* Copyright (c) 2024 Vegard IT GmbH and others. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Initial code from https://github.com/microsoft/vscode/ | ||
* Initial copyright Copyright (C) Microsoft Corporation. All rights reserved. | ||
* Initial license: MIT | ||
* | ||
* Contributors: | ||
* - Microsoft Corporation: Initial code, written in TypeScript, licensed under MIT license | ||
* - Sebastian Thomschke - translation and adaptation to Java | ||
*/ | ||
package org.eclipse.tm4e.languageconfiguration.internal.supports; | ||
|
||
import org.eclipse.tm4e.languageconfiguration.internal.model.IndentationRules; | ||
|
||
/** | ||
* @see <a href= | ||
* "https://github.com/microsoft/vscode/blob/main/src/vs/editor/common/languages/supports/indentRules.ts"> | ||
* github.com/microsoft/vscode/blob/main/src/vs/editor/common/languages/supports/indentRules.ts</a> | ||
*/ | ||
public class IndentRulesSupport { | ||
|
||
public static final class IndentConsts { | ||
public static final int INCREASE_MASK = 0b00000001; | ||
public static final int DECREASE_MASK = 0b00000010; | ||
public static final int INDENT_NEXTLINE_MASK = 0b00000100; | ||
public static final int UNINDENT_MASK = 0b00001000; | ||
} | ||
|
||
private final IndentationRules _indentationRules; | ||
|
||
public IndentRulesSupport(IndentationRules indentationRules) { | ||
this._indentationRules = indentationRules; | ||
} | ||
|
||
public boolean shouldIncrease(final String text) { | ||
return _indentationRules.increaseIndentPattern.matchesFully(text); | ||
|
||
// if (this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(text)) { | ||
// return true; | ||
// } | ||
} | ||
|
||
public boolean shouldDecrease(final String text) { | ||
return _indentationRules.decreaseIndentPattern.matchesFully(text); | ||
} | ||
|
||
public boolean shouldIndentNextLine(final String text) { | ||
return this._indentationRules.indentNextLinePattern != null && this._indentationRules.indentNextLinePattern.matchesFully(text); | ||
} | ||
|
||
public boolean shouldIgnore(final String text) { | ||
// the text matches `unIndentedLinePattern` | ||
return this._indentationRules.unIndentedLinePattern != null && this._indentationRules.unIndentedLinePattern.matchesFully(text); | ||
} | ||
|
||
public int getIndentMetadata(final String text) { | ||
int ret = 0; | ||
if (this.shouldIncrease(text)) { | ||
ret += IndentConsts.INCREASE_MASK; | ||
} | ||
if (this.shouldDecrease(text)) { | ||
ret += IndentConsts.DECREASE_MASK; | ||
} | ||
if (this.shouldIndentNextLine(text)) { | ||
ret += IndentConsts.INDENT_NEXTLINE_MASK; | ||
} | ||
if (this.shouldIgnore(text)) { | ||
ret += IndentConsts.UNINDENT_MASK; | ||
} | ||
return ret; | ||
} | ||
} |
Oops, something went wrong.