private void validateRegions() { if (region_map == null) return; synchronized (region_map) { if (regions_valid) return; Segment s = new Segment(); region_map.clear(); for (BaleRegion br : fragment_regions) { RegionData rd = new RegionData(indent_size); region_map.put(br, rd); int soff = br.getStart(); int eoff = br.getEnd(); if (eoff < soff) continue; try { base_document.getText(soff, eoff - soff, s); } catch (BadLocationException e) { BoardLog.logE("BALE", "Problem getting region indentation", e); continue; } int ln = s.length(); LineData ld; boolean sol = true; for (int i = 0; i < ln; ++i) { char c = s.charAt(i); if (sol) { ld = computeLineData(i, s); rd.add(ld); sol = false; } if (c == '\n') sol = true; } if (!sol) { ld = computeLineData(ln, s); rd.add(ld); ++ln; } } regions_valid = true; } }
/** ***************************************************************************** */ private LineData computeLineData(int offset, Segment s) { int delchar = 0; int addchar = 0; int indent = 0; int ln = s.length(); boolean havetab = false; for (int i = offset; indent < indent_size; ++i) { char c = (i < ln ? s.charAt(i) : '\n'); if (c == ' ') { ++indent; ++delchar; } else if (c == '\t') { havetab = true; indent = nextTabPosition(indent); if (indent > indent_size) { delchar = (i - offset + 1); addchar = indent - indent_size; break; } else { ++delchar; } } else if (c == '\n') { if (havetab) { delchar = (i - offset); addchar = indent_size; } else { addchar = indent_size - (i - offset); } break; } else { BoardLog.logE("BALE", "Minimum space compute incorrectly"); break; } } return new LineData(offset, addchar, delchar); }
boolean getText(Segment brtext, int voff, int len, Segment s) { s.array = new char[len + 1]; s.count = 0; s.offset = 0; if (len == 0) return false; int spos = 0; int line = 0; // first find the line where the data starts for (int i = 1; i < num_lines; ++i) { int npos = spos + line_data[i].getOffset() - line_data[i - 1].getOffset() + line_data[i - 1].getViewDelta(); if (voff < npos) break; spos = npos; line = i; } // now add the data int pos = spos; for (int i = line + 1; i < num_lines; ++i) { int rpos = line_data[i - 1].getOffset(); rpos += line_data[i - 1].getDelete(); int npos = spos + line_data[i].getOffset() - line_data[i - 1].getOffset() + line_data[i - 1].getViewDelta(); int addct = (line_data[i - 1].getDelete() > 0 ? line_data[i - 1].getAdd() : 0); while (pos < npos) { char c; if (pos - spos < addct) c = ' '; else c = brtext.charAt(rpos + pos - spos - addct); if (pos >= voff) s.array[s.count++] = c; if (s.count == len) break; ++pos; } spos = npos; if (s.count == len) break; } boolean lastbad = false; if (s.count < len && num_lines > 0) { if (pos < voff) pos = voff; int rpos = line_data[num_lines - 1].getOffset(); rpos += line_data[num_lines - 1].getDelete(); int addct = line_data[num_lines - 1].getAdd(); while (s.count < len) { char c; int idx = rpos + pos - spos - addct; if (pos - spos < addct) c = ' '; else if (idx < brtext.length()) c = brtext.charAt(idx); else { lastbad = true; c = '\n'; } s.array[s.count++] = c; ++pos; } } else if (s.count < len) { while (s.count < len) { s.array[s.count++] = '\n'; } lastbad = true; } return lastbad; }
/** ***************************************************************************** */ private void setupSpacing() { // get the tab_size from appropriate property BoardProperties bp = BoardProperties.getProperties("Bale"); String v = bp.getProperty("indent.tabulation.size"); if (v == null) v = BumpClient.getBump().getOption("org.eclipse.jdt.core.formatter.tabulation.size"); if (v == null) v = bp.getProperty("Bale.tabsize"); tab_size = 8; try { if (v != null) tab_size = Integer.parseInt(v); } catch (NumberFormatException e) { } region_map = null; regions_valid = true; indent_size = 0; indent_string = null; // determine minimum indent Segment s = new Segment(); int minsp = -1; for (BaleRegion br : fragment_regions) { int soff = br.getStart(); int eoff = br.getEnd(); try { base_document.getText(soff, eoff - soff, s); } catch (BadLocationException e) { BoardLog.logE( "BALE", "Problem getting region text " + soff + " " + eoff + " " + s.length() + " " + base_document.getLength(), e); // should this be "throw e;" continue; } int ln = s.length(); boolean sol = true; int ind = 0; for (int i = 0; i < ln && (minsp < 0 || minsp >= MIN_INDENT); ++i) { char c = s.charAt(i); if (sol) { switch (c) { case ' ': ind += 1; break; case '\t': ind = nextTabPosition(ind); break; case '\n': ind = 0; break; default: if (minsp < 0 || minsp > ind) minsp = ind; sol = false; break; } } else if (c == '\n') { sol = true; ind = 0; } } if (minsp >= 0 && minsp < MIN_INDENT) break; } if (minsp <= 0 || minsp < MIN_INDENT) return; indent_size = minsp; indent_string = ""; for (int i = 0; i < minsp; ++i) indent_string += " "; region_map = new HashMap<BaleRegion, RegionData>(); regions_valid = false; for (BaleRegion br : fragment_regions) { for (int i = getRegionLength(br) - 1; i >= 0; --i) { fixupOffset(br, i, true); } } }