Skip to content
Open
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
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
println!("cargo:rerun-if-changed=src/ffi.rs");
println!("cargo:rerun-if-changed=mime-rs/ffi.rs");

cxx_build::bridge("mime-rs/ffi.rs")
.includes(["c++/vendor/immer", "c++/include"])
Expand Down
41 changes: 41 additions & 0 deletions mime-py/.github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: PyO3/maturin-action@v1
with:
manylinux: auto
command: build
args: --release --sdist -o dist --find-interpreter
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

release:
name: Release
runs-on: ubuntu-latest
if: "startsWith(github.ref, 'refs/tags/')"
needs: linux
steps:
- uses: actions/download-artifact@v3
with:
name: wheels
- name: Publish to PyPI
uses: PyO3/maturin-action@v1
env:
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
with:
command: upload
args: --skip-existing *
72 changes: 72 additions & 0 deletions mime-py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/target

# Byte-compiled / optimized / DLL files
__pycache__/
.pytest_cache/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
.venv/
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
include/
man/
venv/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
pip-selfcheck.json

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

.DS_Store

# Sphinx documentation
docs/_build/

# PyCharm
.idea/

# VSCode
.vscode/

# Pyenv
.python-version
13 changes: 13 additions & 0 deletions mime-py/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "mime-py"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "mime"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.18.2", features = ["extension-module"] }
mime-rs = { path = "../" }
23 changes: 23 additions & 0 deletions mime-py/mime-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import mime


def run() -> None:
doc_view = mime.open("main.go")
nav_view = doc_view.clone()

while nav_view.find("func "):
nav_view.set_mark()
if not nav_view.find("("):
raise RuntimeError("Can't find an open paranthesis after func keyword")

nav_view.backward(1)
fnname = nav_view.copy()

doc_view.paste(f"// Python: {fnname};\n")

doc_view.paste("\n")
print(doc_view.get_contents())
doc_view.save_as("mainout.go")


run()
14 changes: 14 additions & 0 deletions mime-py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[build-system]
requires = ["maturin>=0.14,<0.15"]
build-backend = "maturin"

[project]
name = "mime-py"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]


188 changes: 188 additions & 0 deletions mime-py/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
use pyo3::prelude::*;

#[pyclass]
struct Window(::mime::Window);

#[pyclass]
#[derive(Clone)]
struct Text(::mime::Text);

#[pymethods]
impl Text {
fn __str__(&self) -> String {
self.0.to_string()
}
}

#[derive(FromPyObject)]
enum StringOrText {
String(String),
Text(Text),
}

#[pymethods]
impl Window {
#[staticmethod]
pub fn new() -> Self {
Self(::mime::Window::new())
}

#[staticmethod]
pub fn open(filename: &str) -> Self {
Self(::mime::Window::open(filename))
}

pub fn clone(&self) -> Self {
Window(self.0.clone())
}

pub fn empty(&self) -> bool {
self.0.empty()
}

pub fn size(&self) -> usize {
self.0.size()
}

pub fn narrowed(&self) -> bool {
self.0.narrowed()
}

pub fn save_as(&self, filename: &str) {
self.0.save_as(filename)
}

pub fn set_mark(&self) {
self.0.set_mark()
}

pub fn get_mark(&self) -> Option<i64> {
self.0.get_mark()
}

pub fn get_contents(&self) -> Text {
Text(self.0.get_contents().into())
}

pub fn find(&self, text: &str) -> Option<i64> {
self.0.find(text)
}

pub fn rfind(&self, text: &str) -> Option<i64> {
self.0.rfind(text)
}

pub fn replace(&self, from: &str, to: &str, n: usize) -> i32 {
self.0.replace(from, to, n)
}

pub fn copy(&self) -> Text {
Text(self.0.copy().into())
}

pub fn cut(&self) -> Text {
Text(self.0.cut().into())
}

pub fn paste(&self, text: StringOrText) {
match text {
StringOrText::String(ref string) => self.0.paste(string.as_str()),
StringOrText::Text(text) => self.0.paste(text.0),
}
// self.0.paste(t)
}

pub fn erase_region(&self) {
self.0.erase_region()
}

pub fn clear(&self) {
self.0.clear()
}

pub fn get_pos(&self) -> usize {
self.0.get_pos()
}

pub fn goto_pos(&self, pos: i64) -> bool {
self.0.goto_pos(pos)
}

pub fn del_backward(&self, n: usize) -> usize {
self.0.del_backward(n)
}

pub fn del_forward(&self, n: usize) -> usize {
self.0.del_forward(n)
}

pub fn backward(&self, n: usize) -> usize {
self.0.backward(n)
}

pub fn forward(&self, n: usize) -> usize {
self.0.forward(n)
}

pub fn prev_line(&self, n: usize) -> usize {
self.0.prev_line(n)
}

pub fn next_line(&self, n: usize) -> usize {
self.0.next_line(n)
}

pub fn start_of_buffer(&self) {
self.0.start_of_buffer()
}

pub fn end_of_buffer(&self) {
self.0.end_of_buffer()
}

pub fn start_of_line(&self) {
self.0.start_of_line()
}

pub fn end_of_line(&self) {
self.0.end_of_line()
}

pub fn start_of_block(&self) -> bool {
self.0.start_of_block()
}

pub fn end_of_block(&self) -> bool {
self.0.end_of_block()
}

pub fn narrow_to_block(&self) -> bool {
self.0.narrow_to_block()
}

pub fn narrow_to_region(&self) -> bool {
self.0.narrow_to_region()
}

pub fn widen(&self) {
self.0.widen()
}
}

#[pyfunction]
fn new() -> Window {
Window::new()
}

#[pyfunction]
fn open(name: &str) -> Window {
Window::open(name)
}

/// A Python module implemented in Rust.
#[pymodule]
fn mime(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(new, m)?)?;
m.add_function(wrap_pyfunction!(open, m)?)?;
Ok(())
}
Loading