Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formatting/#785): Default format provider when no extension provider #2022

Merged
merged 9 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Core/Indentation.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
* Helpers for dealing with indentation level
*/

open Utility;

let getLeadingWhitespace = (s: string) => {
let rec loop = (i, spaces, tabs) =>
if (i >= String.length(s)) {
(spaces, tabs, false);
} else {
switch (s.[i]) {
| ' ' => loop(i + 1, spaces + 1, tabs)
| '\t' => loop(i + 1, spaces, tabs + 1)
| _ => (spaces, tabs, true)
};
};

loop(0, 0, 0);
};

let getLevel = (settings: IndentationSettings.t, text: string) => {
let tabSize = settings.tabSize;

Expand Down Expand Up @@ -38,6 +55,22 @@ let getLevel = (settings: IndentationSettings.t, text: string) => {
allWhitespace^ ? 0 : indentLevel^;
};

let applyLevel =
(~indentation: IndentationSettings.t, ~level: int, str: string) => {
str
|> StringEx.findNonWhitespace
|> Option.map(idx => {
let desiredWhitespace =
switch (indentation.mode) {
| Tabs => String.make(level, '\t')
| Spaces => String.make(level * indentation.size, ' ')
};

desiredWhitespace ++ String.sub(str, idx, String.length(str) - idx);
})
|> Option.value(~default=str);
};

let getForBuffer = (~buffer, configuration: Configuration.t) => {
let bufferIndentation = Buffer.getIndentation(buffer);
switch (bufferIndentation) {
Expand Down
20 changes: 3 additions & 17 deletions src/Core/IndentationGuesser.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ module Constants = {
let minSpaces = 2;
};

let getLeadingWhitespace = (s: string) => {
let rec loop = (i, spaces, tabs) =>
if (i >= String.length(s)) {
(spaces, tabs, false);
} else {
switch (s.[i]) {
| ' ' => loop(i + 1, spaces + 1, tabs)
| '\t' => loop(i + 1, spaces, tabs + 1)
| _ => (spaces, tabs, true)
};
};

loop(0, 0, 0);
};

type t = {
mode: IndentationSettings.mode,
size: int,
Expand Down Expand Up @@ -62,7 +47,8 @@ let guessIndentation =
let line = getLine(i);
let prevLine = i == 0 ? "" : getLine(i - 1);

let (spaceCount, tabCount, foundChar) = getLeadingWhitespace(line);
let (spaceCount, tabCount, foundChar) =
Indentation.getLeadingWhitespace(line);

/* Only consider lines with non-whitespace */
if (foundChar) {
Expand All @@ -75,7 +61,7 @@ let guessIndentation =
};

let (prevSpaceCount, _, prevFoundChar) =
getLeadingWhitespace(prevLine);
Indentation.getLeadingWhitespace(prevLine);
if (prevFoundChar) {
let diff = abs(prevSpaceCount - spaceCount);
if (diff >= Constants.minSpaces) {
Expand Down
89 changes: 63 additions & 26 deletions src/Core/LanguageConfiguration.re
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ module BracketPair = {
let endsWithOpenPair = ({openPair, _}, str) => {
StringEx.endsWith(~postfix=openPair, str);
};

let isJustClosingPair = ({closePair, _}, str) => {
let len = String.length(str);

let rec loop = (foundPair, idx) =>
if (idx >= len) {
foundPair;
} else if (foundPair) {
false;
// We found the closing pair... but there's other stuff after
} else {
let c = str.[idx];

if (c == ' ' || c == '\t') {
loop(foundPair, idx + 1);
} else if (c == closePair.[0]) {
loop(true, idx + 1);
} else {
false;
};
};

if (String.length(closePair) == 1) {
loop(false, 0);
} else {
false;
};
};
};

let defaultBrackets: list(BracketPair.t) =
Expand Down Expand Up @@ -226,36 +254,45 @@ let toVimAutoClosingPairs = (syntaxScope: SyntaxScope.t, configuration: t) => {
);
};

let toAutoIndent =
let shouldIncreaseIndent =
(
{increaseIndentPattern, decreaseIndentPattern, brackets, _},
~previousLine as str,
~beforePreviousLine as _,
{increaseIndentPattern, brackets, _},
) => {
let increase =
increaseIndentPattern
|> Option.map(regex => OnigRegExp.test(str, regex))
// If no indentation pattern, fall-back to bracket pair
|> OptionEx.or_lazy(() =>
Some(
List.exists(
bracket => BracketPair.endsWithOpenPair(bracket, str),
brackets,
),
)
increaseIndentPattern
|> Option.map(regex => OnigRegExp.test(str, regex))
// If no indentation pattern, fall-back to bracket pair
|> OptionEx.or_lazy(() =>
Some(
List.exists(
bracket => BracketPair.endsWithOpenPair(bracket, str),
brackets,
),
)
|> Option.value(~default=false);

let decrease =
decreaseIndentPattern
|> Option.map(regex => OnigRegExp.test(str, regex))
|> Option.value(~default=false);

if (increase) {
Vim.AutoIndent.IncreaseIndent;
} else if (decrease) {
Vim.AutoIndent.DecreaseIndent;
} else {
Vim.AutoIndent.KeepIndent;
)
|> Option.value(~default=false);
};

let shouldDecreaseIndent = (~line, {decreaseIndentPattern, brackets, _}) => {
decreaseIndentPattern
|> Option.map(regex => OnigRegExp.test(line, regex))
|> OptionEx.or_lazy(() => {
Some(
List.exists(
bracket => BracketPair.isJustClosingPair(bracket, line),
brackets,
),
)
})
|> Option.value(~default=false);
};

let toAutoIndent = (languageConfig, ~previousLine, ~beforePreviousLine) => {
let increase =
shouldIncreaseIndent(~previousLine, ~beforePreviousLine, languageConfig);

if (increase) {Vim.AutoIndent.IncreaseIndent} else {
Vim.AutoIndent.KeepIndent
};
};
5 changes: 5 additions & 0 deletions src/Core/LanguageConfiguration.rei
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ type t = {

let default: t;

let shouldIncreaseIndent:
(~previousLine: string, ~beforePreviousLine: option(string), t) => bool;

let shouldDecreaseIndent: (~line: string, t) => bool;

let decode: Json.decoder(t);

let toVimAutoClosingPairs: (SyntaxScope.t, t) => Vim.AutoClosingPairs.t;
Expand Down
26 changes: 18 additions & 8 deletions src/Core/Utility/StringEx.re
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,29 @@ let trimRight = str => {
aux(length - 1);
};

let indentation = str => {
let rec loop = i =>
if (i >= String.length(str)) {
i;
} else if (isSpace(str.[i])) {
loop(i + 1);
let findNonWhitespace = str => {
let len = String.length(str);
let rec loop = idx =>
if (idx >= len) {
None;
} else {
i;
let char = str.[idx];
if (char != '\t' && char != ' ') {
Some(idx);
} else {
loop(idx + 1);
};
};

loop(0);
};

let isOnlyWhitespace = str => {
switch (findNonWhitespace(str)) {
| None => true
| Some(_) => false
};
};

let extractSnippet = (~maxLength, ~charStart, ~charEnd, text) => {
let originalLength = String.length(text);

Expand Down
Loading