summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2022-07-14 10:53:39 -0700
committerGitHub <noreply@github.com>2022-07-14 10:53:39 -0700
commit5949e2c5c0c205d5dafeabe88646e96b2a60ec9e (patch)
tree77bf07d8b34ecedea106b177ee92a1ec48f9aa82 /source
parent4af61e2296a49876c2d9e7cf192ae825302a83de (diff)
Language Server: Auto format bug fixes. (#2326)
Diffstat (limited to 'source')
-rw-r--r--source/slang/slang-language-server-auto-format.cpp19
-rw-r--r--source/slang/slang-language-server-auto-format.h9
-rw-r--r--source/slang/slang-language-server-completion.cpp30
-rw-r--r--source/slang/slang-language-server.cpp30
-rw-r--r--source/slang/slang-language-server.h2
5 files changed, 76 insertions, 14 deletions
diff --git a/source/slang/slang-language-server-auto-format.cpp b/source/slang/slang-language-server-auto-format.cpp
index 8d877eaca..fc6d2a25d 100644
--- a/source/slang/slang-language-server-auto-format.cpp
+++ b/source/slang/slang-language-server-auto-format.cpp
@@ -202,7 +202,24 @@ List<Edit> formatSource(UnownedStringSlice text, Index lineStart, Index lineEnd,
// Never allow clang-format to put the semicolon after `}` in its own line.
if (edt.offset < text.getLength() && edt.length == 0 && text[edt.offset] == ';' && edt.offset >0 && text[edt.offset - 1] == '}')
continue;
-
+ // If need to preserve line break, turn all edits with a line break into a space.
+ if (options.behavior == FormatBehavior::PreserveLineBreak)
+ {
+ auto originalText = text.subString(edt.offset, edt.length);
+ bool originalHasLineBreak = originalText.indexOf('\n') != -1;
+ bool newHasLineBreak = edt.text.indexOf('\n') != -1;
+ if (originalHasLineBreak == newHasLineBreak)
+ {
+ }
+ else if (!originalHasLineBreak && newHasLineBreak)
+ {
+ edt.text = " ";
+ }
+ else
+ {
+ continue;
+ }
+ }
edits.add(edt);
}
return edits;
diff --git a/source/slang/slang-language-server-auto-format.h b/source/slang/slang-language-server-auto-format.h
index 8bc51a1d3..b30a2f039 100644
--- a/source/slang/slang-language-server-auto-format.h
+++ b/source/slang/slang-language-server-auto-format.h
@@ -13,10 +13,19 @@ struct Edit
String text;
};
+enum class FormatBehavior
+{
+ Standard,
+ PreserveLineBreak,
+};
+
struct FormatOptions
{
String clangFormatLocation;
String style = "{BasedOnStyle: Microsoft}";
+ bool allowLineBreakInOnTypeFormatting = false;
+ bool allowLineBreakInRangeFormatting = false;
+ FormatBehavior behavior = FormatBehavior::Standard;
};
String findClangFormatTool();
diff --git a/source/slang/slang-language-server-completion.cpp b/source/slang/slang-language-server-completion.cpp
index adb30df6e..93b10b816 100644
--- a/source/slang/slang-language-server-completion.cpp
+++ b/source/slang/slang-language-server-completion.cpp
@@ -107,6 +107,16 @@ List<LanguageServerProtocol::TextEditCompletionItem> CompletionContext::gatherFi
Index sectionEnd,
char closingChar)
{
+ auto realPrefix = prefixPath.getUnownedSlice();
+ while (realPrefix.startsWith(".."))
+ {
+ realPrefix = realPrefix.tail(2);
+ if (realPrefix.startsWith("/") || realPrefix.startsWith("\\"))
+ {
+ realPrefix = realPrefix.tail(1);
+ }
+ }
+
struct FileEnumerationContext
{
List<LanguageServerProtocol::TextEditCompletionItem> items;
@@ -132,7 +142,8 @@ List<LanguageServerProtocol::TextEditCompletionItem> CompletionContext::gatherFi
auto addCandidate = [&](const String& path)
{
context.path = path;
- if (path.getUnownedSlice().endsWithCaseInsensitive(prefixPath.getUnownedSlice()))
+ Path::getCanonical(context.path, context.path);
+ if (path.getUnownedSlice().endsWithCaseInsensitive(realPrefix))
{
OSFileSystem::getExtSingleton()->enumeratePathContents(
path.getBuffer(),
@@ -253,12 +264,21 @@ List<LanguageServerProtocol::TextEditCompletionItem> CompletionContext::gatherFi
}
}
- if (commitCharacterBehavior != CommitCharacterBehavior::Disabled && !isIncomplete)
+ if (!isIncomplete)
{
- for (auto& item : context.items)
+ bool useCommitChars = translateModuleName && (commitCharacterBehavior != CommitCharacterBehavior::Disabled);
+ if (useCommitChars)
{
- for (auto ch : getCommitChars())
- item.commitCharacters.add(ch);
+ if (translateModuleName)
+ {
+ for (auto& item : context.items)
+ {
+ for (auto ch : getCommitChars())
+ {
+ item.commitCharacters.add(ch);
+ }
+ }
+ }
}
}
return context.items;
diff --git a/source/slang/slang-language-server.cpp b/source/slang/slang-language-server.cpp
index 673b91593..768fee9e5 100644
--- a/source/slang/slang-language-server.cpp
+++ b/source/slang/slang-language-server.cpp
@@ -119,6 +119,7 @@ SlangResult LanguageServer::parseNextMessage()
result.capabilities.documentOnTypeFormattingProvider.firstTriggerCharacter = "}";
result.capabilities.documentOnTypeFormattingProvider.moreTriggerCharacter.add(";");
result.capabilities.documentOnTypeFormattingProvider.moreTriggerCharacter.add(":");
+ result.capabilities.documentOnTypeFormattingProvider.moreTriggerCharacter.add("{");
result.capabilities.documentRangeFormattingProvider = true;
result.capabilities.completionProvider.triggerCharacters.add(".");
result.capabilities.completionProvider.triggerCharacters.add(":");
@@ -161,15 +162,15 @@ SlangResult LanguageServer::parseNextMessage()
if (response.result.getKind() == JSONValue::Kind::Array)
{
auto arr = m_connection->getContainer()->getArray(response.result);
- if (arr.getCount() == 9)
+ if (arr.getCount() == 11)
{
updatePredefinedMacros(arr[0]);
updateSearchPaths(arr[1]);
updateSearchInWorkspace(arr[2]);
updateCommitCharacters(arr[3]);
- updateFormattingOptions(arr[4], arr[5]);
- updateInlayHintOptions(arr[6], arr[7]);
- updateTraceOptions(arr[8]);
+ updateFormattingOptions(arr[4], arr[5], arr[6], arr[7]);
+ updateInlayHintOptions(arr[8], arr[9]);
+ updateTraceOptions(arr[10]);
}
}
break;
@@ -1111,9 +1112,15 @@ SlangResult LanguageServer::rangeFormatting(const LanguageServerProtocol::Docume
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
+ Index endLine, endCol;
+ doc->zeroBasedUTF16LocToOneBasedUTF8Loc(args.range.end.line, args.range.end.character, endLine, endCol);
+ Index endOffset = doc->getOffset(endLine, endCol);
if (m_formatOptions.clangFormatLocation.getLength() == 0)
m_formatOptions.clangFormatLocation = findClangFormatTool();
- auto edits = formatSource(doc->getText().getUnownedSlice(), args.range.start.line, args.range.end.line, -1, m_formatOptions);
+ auto options = m_formatOptions;
+ if (!m_formatOptions.allowLineBreakInRangeFormatting)
+ options.behavior = FormatBehavior::PreserveLineBreak;
+ auto edits = formatSource(doc->getText().getUnownedSlice(), args.range.start.line, args.range.end.line, endOffset, options);
auto textEdits = translateTextEdits(doc, edits);
m_connection->sendResult(&textEdits, responseId);
return SLANG_OK;
@@ -1138,7 +1145,10 @@ SlangResult LanguageServer::onTypeFormatting(const LanguageServerProtocol::Docum
Index line, col;
doc->zeroBasedUTF16LocToOneBasedUTF8Loc(args.position.line, args.position.character, line, col);
auto cursorOffset = doc->getOffset(line, col);
- auto edits = formatSource(doc->getText().getUnownedSlice(), args.position.line, args.position.line, cursorOffset, m_formatOptions);
+ auto options = m_formatOptions;
+ if (!m_formatOptions.allowLineBreakInOnTypeFormatting)
+ options.behavior = FormatBehavior::PreserveLineBreak;
+ auto edits = formatSource(doc->getText().getUnownedSlice(), args.position.line, args.position.line, cursorOffset, options);
auto textEdits = translateTextEdits(doc, edits);
m_connection->sendResult(&textEdits, responseId);
return SLANG_OK;
@@ -1270,12 +1280,14 @@ void LanguageServer::updateCommitCharacters(const JSONValue& jsonValue)
}
}
-void LanguageServer::updateFormattingOptions(const JSONValue& clangFormatLoc, const JSONValue& clangFormatStyle)
+void LanguageServer::updateFormattingOptions(const JSONValue& clangFormatLoc, const JSONValue& clangFormatStyle, const JSONValue& allowLineBreakOnType, const JSONValue& allowLineBreakInRange)
{
auto container = m_connection->getContainer();
JSONToNativeConverter converter(container, m_connection->getSink());
converter.convert(clangFormatLoc, &m_formatOptions.clangFormatLocation);
converter.convert(clangFormatStyle, &m_formatOptions.style);
+ converter.convert(allowLineBreakOnType, &m_formatOptions.allowLineBreakInOnTypeFormatting);
+ converter.convert(allowLineBreakInRange, &m_formatOptions.allowLineBreakInRangeFormatting);
if (m_formatOptions.style.getLength() == 0)
m_formatOptions.style = Slang::FormatOptions().style;
}
@@ -1332,6 +1344,10 @@ void LanguageServer::sendConfigRequest()
args.items.add(item);
item.section = "slang.format.clangFormatStyle";
args.items.add(item);
+ item.section = "slang.format.allowLineBreakChangesInOnTypeFormatting";
+ args.items.add(item);
+ item.section = "slang.format.allowLineBreakChangesInRangeFormatting";
+ args.items.add(item);
item.section = "slang.inlayHints.deducedTypes";
args.items.add(item);
item.section = "slang.inlayHints.parameterNames";
diff --git a/source/slang/slang-language-server.h b/source/slang/slang-language-server.h
index 26894b3bd..e7a5f3244 100644
--- a/source/slang/slang-language-server.h
+++ b/source/slang/slang-language-server.h
@@ -141,7 +141,7 @@ private:
void updateSearchPaths(const JSONValue& value);
void updateSearchInWorkspace(const JSONValue& value);
void updateCommitCharacters(const JSONValue& value);
- void updateFormattingOptions(const JSONValue& clangFormatLoc, const JSONValue& clangFormatStyle);
+ void updateFormattingOptions(const JSONValue& clangFormatLoc, const JSONValue& clangFormatStyle, const JSONValue& allowLineBreakOnType, const JSONValue& allowLineBreakInRange);
void updateInlayHintOptions(const JSONValue& deducedTypes, const JSONValue& parameterNames);
void updateTraceOptions(const JSONValue& value);