Skip to content

Commit

Permalink
refactor: add Pre-commit + editorconfig + gitattributes (PLC-lang#1034)
Browse files Browse the repository at this point in the history
* Fix indentation levels cause by tabs

In many places indentation level is little of because tabs where used.
Probably someone might have example tabs size 2. Let's fix these by hand
as automatic tools cannot fix these for us. They can prevent them from
happening but fixing is not easy.

* Fix unicode replacement chars

These seems like accidents so remove those so we can start verify whole
repo against those. In combined.st I changed language to english as it
seems it should be.

* Change all line-endings to LF in whole project

There where not many files which where with CRLF. So it looks like we
should always use LF. Let's fix couple files and then make sure this
does not happen again. I also have some other problems with some of
these files as I get strange douple newlines.

* Add .gitattributes to force line-ending LF

Force line-endings to LF. It could probably also be auto, but unless
someone complain let's keep it LF. This will help that we have unified
way for line-endings so we do not get those accidental commits where
whole file is just big diff because line-ending change.

* Add .pre-commit-config.yaml and .editorconfig

With these we can automate lot of formatting issues with all files. We
also get cleaner commits as every commit will be check with various
small checkers.

* Add editorconfig extension to devcontainer

Now that we have editorconfig file it make sense to add it also to
VScode as it does not support it out of the box.

* ci: Remove submodules checks as we do not have any

We do not have submodules so we do not need to check those.

* format: Strip trailing whitespaces

We want to get rid of trailing whitespaces completly as they make just git
noice. Much better to start using automated tools to get rid of them once and
not getting them back again. This way git history will be cleaner and review
easyer.

Commit was generated with:

    pre-commit run --all-files trailing-whitespace

* format: File should have exacly one new line end of them

It is good practice that every file has one new line. It is not now days so
mandatory but it also is not nice if file has lot of newlines end of it. We will
use pre-commit which takes automatically care about this so let's fix all.

Commit was generated with:

    pre-commit run --all-files end-of-file-fixer

* format: Run Black for whole repo

Currently we only have one python file but is is nice if we have same formatting
for all files. We will use Black for this.

Commit was generated with:

    pre-commit run --all-files black

* format: Convert tabs to spaces

Project mostly use spaces over tabs. When mixing tabs and spaces this usually
makes formatting issues and also when changing those in commits it will make lot
of git noise. We will force spaces so just convert everything to tabs already.

We did also have some users who tab wide was 2 and some 4. This is very common
in projects to happen. Using pre-commit for these we can basically prevent this.

Commit was generated with:

    pre-commit run --all-files remove-tabs

* Regenerate test snapshots

As we have changed style and example removed whitespaces we need to regenerate
all test snapshots.

Commit was generated with:

    ./pre-py/snap-pre.sh; cargo test --all; ./pre-py/snap-rework.sh; cargo test --all; ./pre-py/snap-rework.sh

---------

Co-authored-by: Kari Argillander <[email protected]>
  • Loading branch information
teksturi and Kari Argillander authored Dec 1, 2023
1 parent 23e6139 commit 5615d80
Show file tree
Hide file tree
Showing 368 changed files with 12,573 additions and 12,489 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ rustflags = [
"-C",
"link-arg=--target=aarch64-linux-gnu",
]
linker = "clang"
linker = "clang"
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ENV DEBIAN_FRONTEND=noninteractive
# # [Optional] Add sudo support for the non-root user
# && apt-get install -y sudo \
# && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
# && chmod 0440 /etc/sudoers.d/$USERNAME
# && chmod 0440 /etc/sudoers.d/$USERNAME

RUN apt-get -y update
RUN apt-get -y install git
Expand Down
71 changes: 36 additions & 35 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
{
"name": "Rust",
"build": {
"dockerfile": "./Dockerfile"
},
// "image": "ghcr.io/plc-lang/rust-llvm:latest",
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"name": "Rust",
"build": {
"dockerfile": "./Dockerfile"
},
// "image": "ghcr.io/plc-lang/rust-llvm:latest",
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"installZsh": "true",
"username": "vscode",
"userUid": "1000",
"userGid": "1000",
"upgradePackages": "true"
}
},
// Set *default* container specific settings.json values on container create.
"customizations": {
"vscode": {
"settings": {
"lldb.executable": "/usr/bin/lldb",
// VS Code don't watch files under ./target
"files.watcherExclude": {
"**/target/**": true
},
"rust-analyzer.checkOnSave.command": "clippy"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"bungcip.better-toml",
"vadimcn.vscode-lldb",
"mutantdino.resourcemonitor",
"rust-lang.rust-analyzer"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "rustc --version",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
"containerUser": "vscode"
},
// Set *default* container specific settings.json values on container create.
"customizations": {
"vscode": {
"settings": {
"lldb.executable": "/usr/bin/lldb",
// VS Code don't watch files under ./target
"files.watcherExclude": {
"**/target/**": true
},
"rust-analyzer.checkOnSave.command": "clippy"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"bungcip.better-toml",
"editorconfig.editorconfig",
"vadimcn.vscode-lldb",
"mutantdino.resourcemonitor",
"rust-lang.rust-analyzer"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "rustc --version",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
"containerUser": "vscode"
}
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[*.{yaml,yml}]
indent_size = 2
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Set default behaviour to force line endings to LF.
* text=auto eol=lf
2 changes: 0 additions & 2 deletions .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true

- name: Run Documentation Build
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Update git permissions
run: |
git config --global --add safe.directory /__w/rusty/rusty
Expand Down
14 changes: 6 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ on:
workflow_dispatch:

jobs:
check:
check:
name: Check
runs-on: ubuntu-latest
container: ghcr.io/plc-lang/rust-llvm:latest
steps:
- uses: actions/checkout@v3
with:
submodules: true

- name: Run Check
run: |
Expand Down Expand Up @@ -49,7 +47,7 @@ jobs:
echo "Build command : ./scripts/build.sh --build --release"
./scripts/build.sh --build --release --package \
--target x86_64-unknown-linux-gnu,aarch64-unknown-linux-gnu
- uses: actions/upload-artifact@master
with:
name: plc
Expand Down Expand Up @@ -85,7 +83,7 @@ jobs:
with:
version: ${{ env.llvm-version }}
directory: "./llvm"

- name: Cargo test (Unit)
uses: actions-rs/cargo@v1
with:
Expand All @@ -109,7 +107,7 @@ jobs:
with:
command: build
args: --release

- uses: actions/upload-artifact@master
with:
name: plc.exe
Expand All @@ -127,7 +125,7 @@ jobs:
./scripts/build.sh --check-style
coverage:
name: Run Coverage
name: Run Coverage
runs-on: ubuntu-latest
container: ghcr.io/plc-lang/rust-llvm:latest
steps:
Expand All @@ -140,7 +138,7 @@ jobs:
- name: Upload to codecov.io
uses: codecov/codecov-action@v3
with:
files: lcov.info
files: lcov.info

- name: Archive code coverage results
uses: actions/upload-artifact@v1
Expand Down
75 changes: 75 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Pre-commit is currently in testing phase for this repo. It is not yet forced
# in CI or mandatory to use. Pre-commit will run various checks and mostly fix
# those when you do commiting. You can use it locally by installing it with
#
# pip install pre-commit
#
# and running
#
# pre-commit install
#
# in the root of the repository. This will install git hook which will run
# checks before every commit. If you want to run checks manually for whole repo,
# you can run
#
# pre-commit run --all-files
#
# You can always commit without running checks by adding --no-verify flag to git
# commit command.

exclude: target/|\.git/|.*\.snap

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: c4a0b883114b00d8d76b479c820ce7950211c99b # v4.5.0
hooks:
- id: check-merge-conflict
args: [--assume-in-merge]
# Editorconfig-checker does not care if file contains multiple newlines.
- id: end-of-file-fixer
- id: check-yaml
- id: check-case-conflict
- id: trailing-whitespace
- id: fix-byte-order-marker

- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: 762c66ea96843b54b936fc680162ea67f85ec2d7 # v1.5.4
hooks:
- id: remove-tabs
name: Remove tabs (4 spaces)
args: ['--whitespaces-count', '4']
exclude: \.(yaml|yml)$
- id: remove-tabs
name: Remove tabs (2 spaces)
args: ['--whitespaces-count', '2']
files: \.(yaml|yml)$

- repo: https://github.com/pre-commit/pygrep-hooks
rev: 3a6eb0fadf60b3cccfd80bad9dbb6fae7e47b316 # v1.10.0
hooks:
- id: text-unicode-replacement-char
exclude: ".*test_fdx_utf8.cpp$"

# Formatting Python code. We have not so many files so no example flake8 yet.
- repo: https://github.com/psf/black
rev: 2a1c67e0b2f81df602ec1f6e7aeb030b9709dc7c #23.11.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/doublify/pre-commit-rust
rev: eeee35a89e69d5772bdee97db1a6a898467b686e #v1.0
hooks:
- id: fmt
- id: cargo-check
- id: clippy
args: ["--", "-D", "warnings"]

- repo: https://github.com/editorconfig-checker/editorconfig-checker.python
rev: 2b74735540f79457a50369e5c17a2c35d52c3298 # 2.7.3
hooks:
- id: editorconfig-checker
args: [
# Saddly we have to disable this, but it just won't work. Maybe some day.
-disable-indent-size,
]
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
"cwd": "${workspaceFolder}"
},
]
}
}
24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
name = "rusty"
version = "0.2.0"
authors = [
"Ghaith Hachem <[email protected]>",
"Mathias Rieder <[email protected]>",
"Ghaith Hachem <[email protected]>",
"Mathias Rieder <[email protected]>",
]
edition = "2021"
readme = "README.md"
Expand Down Expand Up @@ -59,16 +59,16 @@ path = "src/lib.rs"

[workspace]
members = [
"xtask",
"libs/stdlib",
"compiler/plc_driver",
"compiler/plc_ast",
"compiler/plc_diagnostics",
"compiler/plc_project",
"compiler/plc_source",
"compiler/plc_util",
"compiler/plc_xml",
"compiler/plc_derive",
"xtask",
"libs/stdlib",
"compiler/plc_driver",
"compiler/plc_ast",
"compiler/plc_diagnostics",
"compiler/plc_project",
"compiler/plc_source",
"compiler/plc_util",
"compiler/plc_xml",
"compiler/plc_derive",
]
default-members = [".", "compiler/plc_driver", "compiler/plc_xml"]

Expand Down
2 changes: 1 addition & 1 deletion book/iecst.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ begin:"[a-zA-Z_]+#\\-?[0-9_]*",relevance:0},{className:"symbol",
begin:"%(I|Q|M)(X|B|W|D|L)[0-9\\.]*"},{className:"symbol",
begin:"%(I|Q|M)[0-9.]*"
},e.C_NUMBER_MODE,e.COMMENT("//","$"),e.C_BLOCK_COMMENT_MODE,e.COMMENT("\\(\\*","\\*\\)")]
})})());
})})());
2 changes: 1 addition & 1 deletion book/llvm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion book/src/arch/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ While a fully fledged compiler generates machine code as a last step, RuSTy gene
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
└────────────────┘ └────────────┘ └────────────┘ └────────────┘ └────────────┘
```
```
16 changes: 8 additions & 8 deletions book/src/arch/codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Struct types translate direclty to llvm struct datatypes.
We generate a new datatype with the user-type's name for the struct.

```iecst
TYPE MyStruct:
TYPE MyStruct:
STRUCT
a: DINT;
b: INT;
Expand Down Expand Up @@ -162,7 +162,7 @@ For every enum's element we generate a global variable with the element's value.
Array types are generated as fixed sized llvm vector types - note that Array types must be fixed sized in *ST* :

```iecst
TYPE MyArray: ARRAY[0..9] OF INT;
TYPE MyArray: ARRAY[0..9] OF INT;
END_TYPE
VAR_GLOBAL
Expand Down Expand Up @@ -208,19 +208,19 @@ This means that such a multidimensional array must be initialized like a single-

```iecst
VAR_GLOBAL
wrong_array : ARRAY[1..2, 0..3] OF INT := [ [10, 11, 12],
[20, 21, 22],
[30, 31, 32]];
wrong_array : ARRAY[1..2, 0..3] OF INT := [ [10, 11, 12],
[20, 21, 22],
[30, 31, 32]];
END_VAR
```

- *correct*

```iecst
VAR_GLOBAL
correct_array : ARRAY[1..2, 0..3] OF INT := [ 10, 11, 12,
20, 21, 22,
30, 31, 32];
correct_array : ARRAY[1..2, 0..3] OF INT := [ 10, 11, 12,
20, 21, 22,
30, 31, 32];
END_VAR
```

Expand Down
Loading

0 comments on commit 5615d80

Please sign in to comment.